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>
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>