|
-
Apr 12th, 2012, 08:27 AM
#1
Thread Starter
Hyperactive Member
reading xml
Hello,
I have an xml document similar to the what's below. I'm wondering how I can read the document and all of its child nodes. I want to only read the parent node called MYTYPES and disregard MYGRAPH. The code I am using reads the document but only prints out the parent node (Type1 and Type 2, in this example). How do i get all the child nodes, and their children. For example, i would like to print out:
Type 1: Clarion, 10. Pittsburgh, 20
Type 2: Trention, 10. Harrisburgh, 20
Code:
Try
Dim ds As New DataSet
ds.ReadXml(xmlFilePath & "myFile.xml")
For Each r As DataRow In ds.Tables(0).Rows
Debug.Print(r.Item(0).ToString)
Next
Catch ex As Exception
Throw
End Try
Code:
<?xml version="1.0" standalone="yes"?>
<MYPARENT>
<MYTYPES>
<MYTYPE>Type 1</MYTYPE>
<MYLOCATIONS>
<LocationName>Clarion</LocationName>
<Age>10</Age>
</MYLOCATIONS>
<MYLOCATIONS>
<LocationName>Pittsburgh</LocationName>
<Age>20</Age>
</MYLOCATIONS>
</MYTYPES>
<MYTYPES>
<MYTYPE>Type 2</MYTYPE>
<MYLOCATIONS>
<LocationName>Trenton</LocationName>
<Age>10</Age>
</MYLOCATIONS>
<MYLOCATIONS>
<LocationName>Harisburgh</LocationName>
<Age>20</Age>
</MYLOCATIONS>
</MYTYPES>
<MYGRAPH>
<GRAPHNAME>NAME1</GRAPHNAME>
<TOTAL>1</TOTAL>
</MYGRAPH>
<MYGRAPH>
<GRAPHNAME>NAME2</GRAPHNAME>
<TOTAL>2</TOTAL>
</MYGRAPH>
</MYPARENT>
if i was able to help, rate my post!
-
Apr 12th, 2012, 08:40 AM
#2
Re: reading xml
If you're using VS2008 or later with .Net 3.5 then you can do this:-
vbnet Code:
' Dim doc As XDocument = XDocument.Parse(XMLString) Dim types = doc...<MYTYPES>
The types variable would have a reference to an IEnumerable<T> object of XElements.
-
Apr 12th, 2012, 08:44 AM
#3
Thread Starter
Hyperactive Member
Re: reading xml
sorry, i should have specified. for this project, I am using Visual Studio 2005 and .net 2.0
if i was able to help, rate my post!
-
Apr 12th, 2012, 08:49 AM
#4
Re: reading xml
Then use the XmlDocument class instead:-
vbnet Code:
' Dim doc As New XmlDocument doc.LoadXml(XMLString) Dim types = doc.GetElementsByTagName("MYTYPES")
types would be a reference to an XmlNodeList object. Also be sure to import the System.Xml namespace.
-
Apr 12th, 2012, 09:03 AM
#5
Thread Starter
Hyperactive Member
Re: reading xml
thanks, ill give that a try
if i was able to help, rate my post!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|