Solved

VB.Net Read XML Documents

Posted on 2011-03-07
7
212 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
[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
  • 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 63

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
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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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