473,506 Members | 17,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.StartupPat h &
"\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>
Nov 20 '05 #1
4 1681
gb
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.
Nov 20 '05 #2
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
Nov 20 '05 #3
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.StartupPat h &
"\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>

Nov 20 '05 #4
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.StartupPat h &
"\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>

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5354
by: Meir S. | last post by:
I think the following is a bug in XmlTextReader: I need to process large XMLs, that are typically constructed of many small elements nested in the root element. Each inner element represents a...
3
4184
by: Pete | last post by:
I'm trying to read an XML document and write out a slightly modified version using the XmlTextWriter. I'm basically trying to copy all the nodes exactly as they are read and do some text...
4
11349
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
5
9188
by: Geoff Bennett | last post by:
While parsing an XML document, my TextReader instance skips nodes. For example, in this fragment: <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14"> <RelatedEntityRef...
1
1834
by: edi | last post by:
Hi, I have MS .Net Framework v1.1.4322. I have this XML file: <?xml version="1.0" ?> <!--here there are two spaces at the beginning--> <aaa> <a id="1"> <Dept>Finance</Dept>
1
5423
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
2236
by: SHC | last post by:
Hi all, I did the "Build" on the attached code in my VC++ .NET 2003 - Windows XP Pro PC. On the c:\ screen, I got the following: Microsoft Development Environment An unhandled exception of type...
2
3792
by: Q | last post by:
I am feeding XmlTextReader a URL that returns the XML that then gets parsed. The URL forms a query that affects how much data is returned in XML but not the format of the data. The problem is...
1
2505
by: Borgbjerg | last post by:
I've got a multi-threaded server (we are very early in the project, trying to uncover some risks), and I want to send data using XML. The class ServerAdapter is supposed to connect to the server...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.