Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linq to XML - Type Casting

Status
Not open for further replies.

rvBasic

Programmer
Oct 22, 2000
414
0
0
BE
Given the following XML document:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ISO_CCY_CODES>
	<ISO_CURRENCY>
		<ENTITY>AFGHANISTAN</ENTITY>
		<CURRENCY>Afghani</CURRENCY>
		<ALPHABETIC_CODE>AFN</ALPHABETIC_CODE>
		<NUMERIC_CODE>971</NUMERIC_CODE>
		<MINOR_UNIT>2</MINOR_UNIT>
	</ISO_CURRENCY>
	<ISO_CURRENCY>
		<ENTITY>ÅLAND ISLANDS</ENTITY>
		<CURRENCY>Euro</CURRENCY>
		<ALPHABETIC_CODE>EUR</ALPHABETIC_CODE>
		<NUMERIC_CODE>978</NUMERIC_CODE>
		<MINOR_UNIT>2</MINOR_UNIT>
	</ISO_CURRENCY>
</ISO_CCY_CODES>

I could transform it into an annonymous type collection with following code:
Code:
Dim ISOxml As XElement = XElement.Load("D:\VB2010\Projects\con_ISOcodes_ch14\Currency ISO Codes.xml")

        'Put ISO codes into a collection of anonymous types
        Dim iCodes = _
            From code In ISOxml.Descendants("ISO_CURRENCY")
                Select New With {.Country = code.Elements("ENTITY").Value,
                                 .Name = code.Elements("CURRENCY").Value,
                                 .ISOcode = code.Elements("ALPHABETIC_CODE").Value,
                                 .NUMcode = code.Elements("NUMERIC_CODE").Value}
        For Each code In iCodes
            Debug.WriteLine(code.Country & vbTab & code.Name & vbTab & code.ISOcode)
        Next

the resulting output:
Code:
AFGHANISTAN	Afghani	AFN
ÅLAND ISLANDS	Euro	EUR

However, I would like to Type the collection, thefore I defined a class CurrencyCode as follows:
Code:
Public Class CurrencyCode
    Public Property Country As String
    Public Property Name As String
    Public Property ISOcode As String
    Public Property NUMcode As String
End Class
Then I tried to proceed. After much experimenting, I finally managed to get an error free statement:
Code:
'Put ISOcodes into a collection of CurrencyCode Type
        'CurrencyCode must be defined as a class
        Dim iiCodes As CurrencyCode = _
            CType((From code In ISOxml.Descendants("ISO_CURRENCY")
                Select New CurrencyCode With {.Country = code.Elements("ENTITY").Value,
                                 .ISOcode = code.Elements("ALPHABETIC_CODE").Value,
                                 .NUMcode = code.Elements("NUMERIC_CODE").Value}), CurrencyCode)
which ... flunked at run time with following execption:
Code:
Cannot convert an object of type WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,con_ISOcodes_ch14.CurrencyCode] into type con_ISOcodes_ch14.CurrencyCode.

How could I achieve my goal?
Ideally I would like to have a declaration as a typed list:
Code:
Dim iiiCodes() as CurrencyCode = ...
but this seems even farther away

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top