Solved

VB.Net XML Reading

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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