473,545 Members | 2,776 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 GetPlaceParamet ers(ByVal sPlace As String)
Dim xtr As New XmlTextReader(F ile.OpenRead(Ap plication.Start upPath &
"\Textfiles\Top 10Table.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.Ele ment
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.ReadElement String '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>Cottag e</House>
<PLACECode>BB </PLACECode>
<Position>Secon d</Position>
</Site>
Nov 20 '05 #1
4 1685
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.Attribute s.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()="Detache d"]/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.lo ad(Application. StartupPath & "\Textfiles\Top 10Table.xml")
Nodes = xmlDomFileIn.Se lectNodes("//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.publi c.dotnet.langua ges.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 GetPlaceParamet ers(ByVal sPlace As String)
Dim xtr As New XmlTextReader(F ile.OpenRead(Ap plication.Start upPath &
"\Textfiles\Top 10Table.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.Ele ment
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.ReadElement String '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>Cottag e</House>
<PLACECode>BB </PLACECode>
<Position>Secon d</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()="Detache d"]/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.lo ad(Application. StartupPath & "\Textfiles\Top 10Table.xml")
Nodes = xmlDomFileIn.Se lectNodes("//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.publi c.dotnet.langua ges.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 GetPlaceParamet ers(ByVal sPlace As String)
Dim xtr As New XmlTextReader(F ile.OpenRead(Ap plication.Start upPath &
"\Textfiles\Top 10Table.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.Ele ment
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.ReadElement String '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>Cottag e</House>
<PLACECode>BB </PLACECode>
<Position>Secon d</Position>
</Site>

Nov 20 '05 #5

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

Similar topics

4
5357
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 command, so I have to parse and execute them according to the order they appear in the ROOT element. I don't want to use XmlDocument for the...
3
4189
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 manipulation on #text nodes of only certain named elements. example while (xmltextreader.Read()) { if (xmltextreader.NodeType == XmlNodeType.Text)
4
11355
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 should have worked fine since the class in question was already being serialized and deserialized as part of a Web service interface. What I found was...
5
9195
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 RelatedID="118"/> <PersonName> <NameTitle Value="Mr"/> <FirstName>Clint</FirstName> <OtherName/> <AlsoKnownAs>Mr C Eastwood</AlsoKnownAs>
1
1837
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
5433
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> <EmployeeStrength>> 1000</EmployeeStrength> When I do a Xmltextreader.read() and then check the contents of the xml node by...
1
2244
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 'System.Xml.XmlException' occured in system.xml.dll Addtional Information: System error |Break| |Continue| I clicked on the |Continue| and I got...
2
3794
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 that when the URL string exceeds about 163 characters (strange number) XmlTextReader seems to choke on it and it doesn't seem like any XML is actually...
1
2506
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 (which works fine), to send data, but on the same stream be able to read data (which is causing problems). In the function "bool Connect()", we...
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1919
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 we have to send another system
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.