XMLTextReader Problem

  • Thread starter Thread starter Paul Bromley
  • Start date Start date
P

Paul Bromley

I thought that XMLTextReader would be simple to use, but I have run into
problems with it! I seem to have great difficulty extrcting the text of
specific elements from a very simple XML file.

I have a very simple XML file that I wish to parse using Xmltextreader, but
I seem to be having a lot of poblems with it. I have a subroutine that I
pass a string into, and what I need to do is to find the element where that
string exists, and then go on to read the next 2 nodes. Ideally I would like
to check the element names first to make sure that I am getting the correct
data. For example in the XML file below I would like to pass the string
'Detached' in , get the XMLReader to move to this element, and then read
the 2 elements below - PLACECode and Position and put the element contents
into string variables - i.e. BD & Ninth.

Below is the code that I have been trying to get working:-

Public Sub GetPlaceParameters(ByVal sPlace As String)
Dim xtr As New XmlTextReader(File.OpenRead(Application.StartupPath &
"\Textfiles\Top10Table.xml")) 'Create XmlTextReader
Dim xCount As Integer = 0 'Integer for Array Index
Dim sCode As String
Do While Not xtr.EOF 'Read XML File
xtr.Read()
Select Case xtr.NodeType
Case XmlNodeType.Element
If xtr.Name = "House" Then
If xtr.ReadString = sPlace Then 'Check to see if element text is same as
string passed in - e.g. "Detached"
xtr.Read()
**** sCode = xtr.ReadElementString 'Read the contents of the next element
**** I got this to work - but could not get - sCode = xtr.ReadString to work
MsgBox( sCode )
End If
End If '
End Select
Loop
End Sub

What syntax should I be using to move to the next element once I have found
the element containing the string that I have passed in - AND how can I
check that I am reading the correct element. Above - I have tried: above as
marked with ****-

If xtr.Name = "PLACECode" Then
sCode = xtr.ReadString
MsgBox(sCode)
End If


XML file:-

<?xml version="1.0" standalone="yes"?>
<Place xmlns="http://tempuri.org/Place.xsd">
<Site>
<House>Bungalow</House>
<PLACECode>S</PLACECode>
<Position>First</Position>
</Site>
<Site>
<House>Semi Detached</House>
<PLACECode>BA</PLACECode>
<Position>Third</Position>
</Site>
<Site>
<House>Detached</House>
<PLACECode>BD</PLACECode>
<Position>Ninth</Position>
</Site>
<Site>
<House>Cottage</House>
<PLACECode>BB</PLACECode>
<Position>Second</Position>
</Site>
 
You can use ChildNodes of the existing Node to get to the next level nodes

I use the below type comparsion, but there is no reason you cannot simply do value = value type comparsion

If String.Compare(xNode.Attributes.Item(0).Value, strVal, True) = 0 The

Hope this helps.
 
Hi Paul,

When I see it right, than is this an XML dataset.

You can try what the dataset.readXML can do for you, that is much easier to
handle for such simple XML files.

The datareader can handle more complex XML files, and is therefore as well
much more complicated to handle.

I hope this helps?

Cor
 
Hi Paul I would use XPath if I were you. The Path you would use would look
like this

//Site/House[text()="Detached"]/following-sibling::*

This would give you the <House> node that contains the "Detached" text plus
its siblings (the other two nodes)

VB code...

Dim xmlDomFileIn As New XmlDocument
Dim Nodes As XmlNodeList
xmlDomFileIn.load(Application.StartupPath & "\Textfiles\Top10Table.xml")
Nodes = xmlDomFileIn.SelectNodes("//Site/House[text()=" & chr(34) &
"Detached" chr(34) "]/following-sibling::*")
If Nodes.Count <> 0 Then
x = Nodes.Count
For y = 0 To x - 1
'Do your thing here
'Nodes.Item(y).InnerText <-- gives you the node text
i.e "Detached"
'Nodes.Item(y).Name <-- gives you the node name
i.e "House"
next
end if
----- Original Message -----
From: Paul Bromley
Newsgroups: microsoft.public.dotnet.languages.vb
Sent: Monday, May 17, 2004 2:40 PM
Subject: XMLTextReader Problem


I thought that XMLTextReader would be simple to use, but I have run into
problems with it! I seem to have great difficulty extrcting the text of
specific elements from a very simple XML file.

I have a very simple XML file that I wish to parse using Xmltextreader,
but
I seem to be having a lot of poblems with it. I have a subroutine that I
pass a string into, and what I need to do is to find the element where
that
string exists, and then go on to read the next 2 nodes. Ideally I would
like
to check the element names first to make sure that I am getting the
correct
data. For example in the XML file below I would like to pass the string
'Detached' in , get the XMLReader to move to this element, and then read
the 2 elements below - PLACECode and Position and put the element contents
into string variables - i.e. BD & Ninth.

Below is the code that I have been trying to get working:-

Public Sub GetPlaceParameters(ByVal sPlace As String)
Dim xtr As New XmlTextReader(File.OpenRead(Application.StartupPath &
"\Textfiles\Top10Table.xml")) 'Create XmlTextReader
Dim xCount As Integer = 0 'Integer for Array Index
Dim sCode As String
Do While Not xtr.EOF 'Read XML File
xtr.Read()
Select Case xtr.NodeType
Case XmlNodeType.Element
If xtr.Name = "House" Then
If xtr.ReadString = sPlace Then 'Check to see if element text is same as
string passed in - e.g. "Detached"
xtr.Read()
**** sCode = xtr.ReadElementString 'Read the contents of the next element
**** I got this to work - but could not get - sCode = xtr.ReadString to
work
MsgBox( sCode )
End If
End If '
End Select
Loop
End Sub

What syntax should I be using to move to the next element once I have
found
the element containing the string that I have passed in - AND how can I
check that I am reading the correct element. Above - I have tried: above
as
marked with ****-

If xtr.Name = "PLACECode" Then
sCode = xtr.ReadString
MsgBox(sCode)
End If


XML file:-

<?xml version="1.0" standalone="yes"?>
<Place xmlns="http://tempuri.org/Place.xsd">
<Site>
<House>Bungalow</House>
<PLACECode>S</PLACECode>
<Position>First</Position>
</Site>
<Site>
<House>Semi Detached</House>
<PLACECode>BA</PLACECode>
<Position>Third</Position>
</Site>
<Site>
<House>Detached</House>
<PLACECode>BD</PLACECode>
<Position>Ninth</Position>
</Site>
<Site>
<House>Cottage</House>
<PLACECode>BB</PLACECode>
<Position>Second</Position>
</Site>
 
Back
Top