Jessee
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.
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.
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.
Fernando
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
Fernando
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?
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
- <Guild>
- <Ranks>
- <Rank>
<Id>0</Id>
<Name>Test1</Name>
</Rank>
- <Rank>
<Id>1</Id>
<Name>Test2</Name>
</Rank>
</Ranks>
</Guild>
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.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect!
Open in new window
Remembering to add the line "Imports System.Xml" at the top of your code file.