|
-
May 12th, 2011, 09:32 AM
#1
Thread Starter
Hyperactive Member
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.
-
May 12th, 2011, 09:50 AM
#2
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
-
May 12th, 2011, 12:08 PM
#3
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
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
May 12th, 2011, 12:26 PM
#4
Thread Starter
Hyperactive Member
Re: How to read this xml information
Hello Wild Bill,
Thanks for the information.
Nice regards,
Michelle.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|