XGenwareS
asked on
VB.Net XML Reading
I Have a xml file like this:
<Profile>
<IPS>
<IP>ipnumber</IP>
<IP>ipnumber</IP>
<IP>ipnumber</IP>
</IPS>
</Profile>
I have code to read a single entry in the <IPS> section like this:
Dim xmldoc As New XmlDataDocument()
Dim xmlnode As XmlNodeList
Dim i As Integer
Dim fs As New FileStream(xmlf, FileMode.Open, FileAccess.Read)
xmldoc.Load(fs)
xmlnode = xmldoc.GetElementsByTagNam e("IPS")
For i = 0 To xmlnode.Count - 1
TextBox1.Text = xmlnode(i).ChildNodes.Item (0).InnerT ext.Trim()
Next
My question is, how would I take each <IP> listed in <IPS> and add it to a listbox?
<Profile>
<IPS>
<IP>ipnumber</IP>
<IP>ipnumber</IP>
<IP>ipnumber</IP>
</IPS>
</Profile>
I have code to read a single entry in the <IPS> section like this:
Dim xmldoc As New XmlDataDocument()
Dim xmlnode As XmlNodeList
Dim i As Integer
Dim fs As New FileStream(xmlf, FileMode.Open, FileAccess.Read)
xmldoc.Load(fs)
xmlnode = xmldoc.GetElementsByTagNam
For i = 0 To xmlnode.Count - 1
TextBox1.Text = xmlnode(i).ChildNodes.Item
Next
My question is, how would I take each <IP> listed in <IPS> and add it to a listbox?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try
Dim myListbox As New ListBox
Dim nDocument As XDocument = XDocument.Load("C:\test.xml", LoadOptions.PreserveWhitespace)
Dim myString = From kp In ldDocument.Root.Descendants("IPS") Select kp.<IP>.Value
For Each x As String In myString
myListbox.Items.Add(x)
Next
ASKER
I tried this code:
For i = 0 To xmlnode.ChildNodes.Count - 1
yourList.Add(xmlnode(0).Ch ildNodes.I tem(i).Inn erText.Tri m())
Next
and it worked fine when there was only one item in the <IPS> section, but when there was multiple, it only listed the first one.
For i = 0 To xmlnode.ChildNodes.Count - 1
yourList.Add(xmlnode(0).Ch
Next
and it worked fine when there was only one item in the <IPS> section, but when there was multiple, it only listed the first one.
Ok, if there can be more than one IPS, then you'll want something more like:
Dim nodeList As XmlNodeList = xmldoc.SelectNodes("Profile/IPS")
For Each node As XmlNode In nodeList
For i As Integer = 0 To node.ChildNodes.Count - 1
yourList.Add(node.ChildNodes(i).InnerText.Trim())
Next
Next
ASKER