[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB.Net Read XML Documents

Posted on 2011-03-07
7
Medium Priority
?
216 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 64

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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