?
Solved

VB.Net XML Reading

Posted on 2010-08-25
5
Medium Priority
?
255 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:XGenwareS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 33527054
You should be able to use a loop like:
For i = 0 To xmlnode.ChildNodes.Count - 1
   yourList.Add(xmlnode(0).ChildNodes.Item(i).InnerText.Trim())
Next

Open in new window

0
 

Author Closing Comment

by:XGenwareS
ID: 33527067
Thanks worked perfect :)
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33527068
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

0
 

Author Comment

by:XGenwareS
ID: 33527144
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.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33529121
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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question