Link to home
Start Free TrialLog in
Avatar of Jessee
JesseeFlag for Australia

asked on

VB.Net Read XML Documents

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.
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
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
Avatar of Jessee

ASKER

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

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.
Avatar of Jessee

ASKER

Thats not the whole XML.
I have attached the XML file.

 guild.xml
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jessee

ASKER

Perfect!