Solved

VB.Net Read XML Documents

Posted on 2011-03-07
7
210 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 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net help 4 31
VB.NET - Error - Object not set in Module 4 33
Import a txt file into a DataGridView and TextBox 20 38
Complex SQL statement in VB.NET 7 15
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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

777 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