How to read this xml information
Dear all,
We got from our customer a xml file with the next information:
<World>
<Countries>
<Country CODE="A">1</Country>
<Country CODE="B">2</Country>
<Country CODE="BG">3</Country>
<Country CODE="CY">4</Country>
<Country CODE="CZ">5</Country>
</Countries>
</World>
How can we get the the values for the country codes?
Any help is welcome.
Michelle.
Re: How to read this xml information
there are several ways... how are you reading in the data now? - it kind of determines how to get to the data...
-tg
Re: How to read this xml information
You could do something like this
Code:
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim inputXml As String = "<World>" & Environment.NewLine & _
"<Countries>" & Environment.NewLine & _
"<Country CODE=""A"">1</Country>" & Environment.NewLine & _
"<Country CODE=""B"">2</Country>" & Environment.NewLine & _
"<Country CODE=""BG"">3</Country>" & Environment.NewLine & _
"<Country CODE=""CY"">4</Country>" & Environment.NewLine & _
"<Country CODE=""CZ"">5</Country>" & Environment.NewLine & _
"</Countries>" & Environment.NewLine & _
"</World>"
Dim countries = ParseCountryXml(inputXml)
End Sub
Private Function ParseCountryXml(ByVal inputXml As String) As List(Of Country)
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.LoadXml(inputXml)
Dim countries As New List(Of Country)
For Each countryNode As Xml.XmlElement In xmlDoc.SelectNodes("/World/Countries/Country")
Dim code = countryNode.Attributes("CODE").InnerText
Dim id = CInt(countryNode.InnerText)
countries.Add(New Country(code, id))
Next
Return countries
End Function
Public Class Country
Private codeValue As String
Property Code() As String
Get
Return Me.codeValue
End Get
Set(ByVal value As String)
Me.codeValue = value
End Set
End Property
Private idValue As Integer
Public Property Id() As Integer
Get
Return idValue
End Get
Set(ByVal value As Integer)
idValue = value
End Set
End Property
Public Sub New(ByVal code As String, ByVal id As Integer)
Me.Code = code
Me.Id = id
End Sub
End Class
Re: How to read this xml information
Hello Wild Bill,
Thanks for the information.
Nice regards,
Michelle.