XmlNodeReader

G

Guest

All

I have an XML document as illustrated in the extract below. I am trying to
use the XmlNodeReader to extract information from the document. What I am
having trouble with is when I read the line of the document I am unable to
read the value of the element. Below is an extract of the source code that I
am using to read the other parts of the document that I want to extract. Any
assistance would be appreciated.

Thanks

ie. What I am trying to extract

<value code="Mr">Mr</value> - want to extract "value"
<value code="Mrs">Mrs</value> - want to extract "value"
.....etc
<value code="NSW">NSW</value> - want to extract "value"
<caption>State:</caption> - want to extract "caption"


--- XML Doc ---

<root>
<selection attr="1">
<value code="Mr">Mr</value>
<value code="Mrs">Mrs</value>
<value code="Ms">Ms</value>
<value code="Miss">Miss</value>
<caption>Title:</caption>
</selection>
<selection attr="2">
<value code="QLD">QLD</value>
<value code="VIC">VIC</value>
<value code="NSW">NSW</value>
<caption>State:</caption>
</selection>
</root>

--- End XML Doc ---

--- Code ---

Dim sr As New System.IO.StreamReader("c:\xml.xml")
Dim sw As New System.IO.StreamWriter("c:\output.txt")
Dim attr As String
Dim doc As New Xml.XmlDocument

doc.Load(sr)
Dim reader As New Xml.XmlNodeReader(doc)

While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "selection" Then
attr = reader.GetAttribute("attr")
End If
Case Xml.XmlNodeType.Text
sw.WriteLine(attr & ", " & reader.Value)
End Select
End While

sw.Close()

--- End Code ---
 
D

Derek Harmon

David said:
I have an XML document as illustrated in the extract below. I am trying to
use the XmlNodeReader to extract information from the document. What I am
having trouble with is when I read the line of the document I am unable to
read the value of the element.

Those are called an element's "local name" (you can only
read the Element's LocalName when the XmlNodeReader
is currently positioned on it, so if you need it later you must
store it in a local variable).

Elements have no "values", but they may have "children".

Once you learn these names the API will become a lot easier. ;-)
<caption>State:</caption> - want to extract "caption"
: :
Dim lastElementName As String = String.Empty
While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "selection" Then
attr = reader.GetAttribute("attr")
End If
lastElementName = reader.LocalName
Case Xml.XmlNodeType.Text
sw.WriteLine( attr & ", " & lastElementName )
End Select
End While


Derek Harmon
 
G

Guest

Derek

Thanks for your responce.

I have tried adding a Message Box to display the local name when I read the
TextNode as illistrated below, and the Message Box is blank. Am I doing
something wrong.

Thanks

Case Xml.XmlNodeType.Text
MsgBox(reader.LocalName)
sw.WriteLine( attr & ", " & lastElementName )
 
N

Nak

Hi David,

The following is some untested code that should get you going. If you
get desparate I'll knock up a working example for you, but I think you
should be able to see what's happening here. The only tricky bit to
understand might be the XPath expressions which can be read about at the
following address

http://www.w3schools.com/xpath/xpath_syntax.asp

-----------

DIm doc as new xmldocument
call doc.load("filename")

Dim nodelist As XmlNodeList = doc.SelectNodes("root/selection[@attr='1']")

Dim i as integer
for i = 0 to nodelist.count -1

Dim nodelist2 as XmlNodeList = nodelist.item(i).SelectNodes("value")

Dim y as integer
for y = 0 to nodelist2.count - 1

nodelist2.item(y).atttributes("code").value

nodelist2.item(y).innertext


next

next

-----------

Nick.

<root>

Select this section using - root/selection[@attr='1']
<selection attr="1">

To select these in 1 I *think* you can do -
root/selection[@attr='1']/value
<value code="Mr">Mr</value>
<value code="Mrs">Mrs</value>
<value code="Ms">Ms</value>
<value code="Miss">Miss</value>

<caption>Title:</caption>
</selection>

Select this section using - root/selection[@attr='2']
<selection attr="2">

To select these in 1 I *think* you can do -
root/selection[@attr='2']/value
<value code="QLD">QLD</value>
<value code="VIC">VIC</value>
<value code="NSW">NSW</value>
<caption>State:</caption>
</selection>

</root>
 
N

Nak

Hi David,

Sorry, didn't mention what the following lines were for

Obtain an attribute
nodelist2.item(y).atttributes("code").value

Obtain the inner text
nodelist2.item(y).innertext

<element attribute="value">InnerText</element>

Nick.

Nak said:
Hi David,

The following is some untested code that should get you going. If you
get desparate I'll knock up a working example for you, but I think you
should be able to see what's happening here. The only tricky bit to
understand might be the XPath expressions which can be read about at the
following address

http://www.w3schools.com/xpath/xpath_syntax.asp

-----------

DIm doc as new xmldocument
call doc.load("filename")

Dim nodelist As XmlNodeList = doc.SelectNodes("root/selection[@attr='1']")

Dim i as integer
for i = 0 to nodelist.count -1

Dim nodelist2 as XmlNodeList = nodelist.item(i).SelectNodes("value")

Dim y as integer
for y = 0 to nodelist2.count - 1

nodelist2.item(y).atttributes("code").value

nodelist2.item(y).innertext


next

next

-----------

Nick.

<root>

Select this section using - root/selection[@attr='1']
<selection attr="1">

To select these in 1 I *think* you can do -
root/selection[@attr='1']/value
<value code="Mr">Mr</value>
<value code="Mrs">Mrs</value>
<value code="Ms">Ms</value>
<value code="Miss">Miss</value>

<caption>Title:</caption>
</selection>

Select this section using - root/selection[@attr='2']
<selection attr="2">

To select these in 1 I *think* you can do -
root/selection[@attr='2']/value
<value code="QLD">QLD</value>
<value code="VIC">VIC</value>
<value code="NSW">NSW</value>
<caption>State:</caption>
</selection>

</root>
 

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

Top