Getting node names from XML

  • Thread starter Thread starter vbmark
  • Start date Start date
V

vbmark

I have the follwing XML string:

<RecordSet>
<Row RowNumber="1">
<UserID>111</UserID>
<Phone>1234567890</Phone>
</Row>
<Row RowNumber="2">
<UserID>747</UserID>
<Phone>9876543210</Phone>
</Row>
</RecordSet>

A <Row> can have a variable number of elements in it.

How do I get a list of the elements? Such as in the
above case would be UserID and Phone?

My goal is to convert the XML into a Dataset.

Thanks!
 
Here's one way:

Public Function GetNodes(ByVal xpathExpression As String) As String()
Dim node As XmlNode = m_root.SelectSingleNode(xpathExpression)
If node Is Nothing Then Return Nothing
Dim nodelist As XmlNodeList = node.ChildNodes
Dim s(nodelist.Count - 1) As String
Dim i As Integer = 0
For Each xnode As XmlNode In nodelist
s(i) = xnode.Name
i += 1
Next
Return s
End Function

HTH
Steve
 
Hi Mark,
Any reason you can't just read the Xml directly into a DataSet, using
the .ReadXml() method?

Marcie
 
What's m_root?

Here's one way:

Public Function GetNodes(ByVal xpathExpression As String) As String()
Dim node As XmlNode = m_root.SelectSingleNode(xpathExpression)
If node Is Nothing Then Return Nothing
Dim nodelist As XmlNodeList = node.ChildNodes
Dim s(nodelist.Count - 1) As String
Dim i As Integer = 0
For Each xnode As XmlNode In nodelist
s(i) = xnode.Name
i += 1
Next
Return s
End Function

HTH
Steve
 
Did it give you an error, or was the data not stored in the way you
wanted it?

Marcie
 
Opps! Sorry. You select that first thing. It's the DocumentElement of the
XmlDocument
Dim m_xmldoc as New XmlDocument
Dim m_root as XmlElement
m_xmldoc.Load(fileNameandPath)
m_root = m_xmldoc.DocumentElement
 
When I do this:

Dim xmlSR As System.IO.StringReader = New System.IO.StringReader(xmlData)

xmlSR is always Nothing.
 
I get an error that xpathExpression is a type and can not be used as an
espression.

Also, instead of passing a path to a file I just want to pass it the
string variable of the XML. How do I do that?
 
This worked fine for me (pardon the line breaks):

Dim sxml As String = "<?xml version=""1.0"" encoding=""utf-8"" ?><RecordSet><Row RowNumber=""1""><UserID>111</UserID><Phone>1234567</Phone></Row><Row
RowNumber=""2""><UserID>747</UserID><Phone>123456</Phone></Row></RecordSet>"

Dim SR As StringReader = New StringReader(sxml)
Dim DS As DataSet = New DataSet
DS.ReadXml(SR)


Marcie
 
Mark, your questions are leading me to wonder if you are even trying to read
the documentation for the code that both myself and Marcie are trying to
give you. For instance, if you read the documentation for the
SelectSingleNode method, you would see that xpathExpression is a valid Xpath
expression. If you need to learn Xpath, there are plenty of places to do
that on the internet beginning with:
http://www.w3schools.com/
Additionally, if you read the documentation for the Load method, you would
see that there are four overloads and one of them is sure to fit your
situation. You can even combine some of mine and Marcie's code to suit your
situation if you like. In fact, the example code for the Load method that
takes a TextReader as an argument, uses a StringReader like Marcie is doing
in her code. So, read up, code up, and have some fun.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top