Given the following XML document:
I could transform it into an annonymous type collection with following code:
the resulting output:
However, I would like to Type the collection, thefore I defined a class CurrencyCode as follows:
Then I tried to proceed. After much experimenting, I finally managed to get an error free statement:
which ... flunked at run time with following execption:
How could I achieve my goal?
Ideally I would like to have a declaration as a typed list:
but this seems even farther away
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
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
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)
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 = ...
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]