Link to home
Start Free TrialLog in
Avatar of XGenwareS
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.GetElementsByTagName("IPS")
  For i = 0 To xmlnode.Count - 1
            TextBox1.Text = xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
        Next

My question is, how would I take each <IP> listed in <IPS> and add it to a listbox?
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XGenwareS
XGenwareS

ASKER

Thanks worked perfect :)
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

Open in new window

I tried this code:
For i = 0 To xmlnode.ChildNodes.Count - 1
   yourList.Add(xmlnode(0).ChildNodes.Item(i).InnerText.Trim())
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

Open in new window