Solved

VB.Net XML Reading

Posted on 2010-08-25
5
245 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
  • 2
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now