Solved

VB.Net XML Reading

Posted on 2010-08-25
5
254 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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