Solved

VB.Net Read XML Documents

Posted on 2011-03-07
7
208 Views
Last Modified: 2012-05-11
Hey,

I know this has most likely been covered multiple times, but I am unable to grasp the concept of reading sub nodes.
For example:
<EG1>
   <EG2>
       <EG3>
        <Data1>
        <Data2>
        </EG3>
        <EG3>
        <Data1>
        <Data2>
        </EG3>
        <EG3>
        <Data1>
        <Data2>
        </EG3>
   </EG2>
</EG1>

What im needing to do is loop through all the EG3, and get the text of data1.

If you need me to clarify on anything, let me know.
0
Comment
Question by:Letsgetcoding
  • 3
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35057264
Well, your XML isn't valid, but i'm going to assume that's just a posting typo. You need something like the following sample to do what you need:
        Dim doc As New XmlDocument()
        doc.Load("C:\test.xml")

        '// grab all "EG3" nodes in the document
        Dim nodes As XmlNodeList = doc.SelectNodes("//EG3")

        For Each node As XmlNode In nodes
            '// output the text value of the first child node (i.e. the "Data1" node)
            Console.WriteLine(node.ChildNodes(0).InnerText)
        Next

Open in new window

Remembering to add the line "Imports System.Xml" at the top of your code file.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 35057965
Hi Letsgetcoding;

If you are using .Net Framework 3.5 SP 1 or above you can use Linq to XML as shown in the code snippet below.

Imports System.Xml.Linq

' Get the Document loaded
Dim xdoc As XDocument = XDocument.Load("Test.xml")

' Get all the Data1 nodes and print out all there inner text
For Each data As XElement In xdoc...<Data1>
    Console.WriteLine(data.Value)
Next

Open in new window


Fernando
0
 

Author Comment

by:Letsgetcoding
ID: 35063998
Ok, so i'm using the code.

The XML is attached.

Running it I get this error:
Object reference not set to an instance of an object.

Why?

Public Sub LoadUserRank(ByVal RankID As String)
        Dim XMLDoc As New XmlDocument

        XMLDoc.Load(frmMain.txtBrowse.Text)

        Dim nodes As XmlNodeList = XMLDoc.SelectNodes("//Rank")

        For Each node As XmlNode In nodes
            If node.ChildNodes(0).InnerText = RankID Then
                frmMain.txtRank.Text = node.ChildNodes(1).InnerText
            End If
        Next
    End Sub

Open in new window

- <Guild>
- <Ranks>
- <Rank>
  <Id>0</Id> 
  <Name>Test1</Name> 
  </Rank>
- <Rank>
  <Id>1</Id> 
  <Name>Test2</Name> 
  </Rank>
  </Ranks>
  </Guild>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35066920
Is that the whole XML? What you have looks correct, which would make me think that there is either more to the XML (i.e. a namespace), or possibly "frmMain" is not a valid object.
0
 

Author Comment

by:Letsgetcoding
ID: 35066971
Thats not the whole XML.
I have attached the XML file.

 guild.xml
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35066991
Ah, ok. The problem lies in the fact that you have "Rank" nodes nested inside "Member" nodes as well as the ones under the "Ranks" nodes. The ones under the "Member" nodes don't have children, hence the call to ChildNodes(0) errors.

Given what you appear to be trying to achieve you can simplify the code by being more explicit in the XPath query.
Try:
Public Sub LoadUserRank(ByVal RankID As String)
        Dim XMLDoc As New XmlDocument

        XMLDoc.Load(frmMain.txtBrowse.Text)

        Dim node As XmlNode = XMLDoc.SelectSingleNode("//Rank[Id='" & RankID & "']")
        If Not node Is Nothing Then
             frmMain.txtRank.Text = node.ChildNodes(1).InnerText
        End If
    End Sub

Open in new window

0
 

Author Closing Comment

by:Letsgetcoding
ID: 35067196
Perfect!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

20 Experts available now in Live!

Get 1:1 Help Now