473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error Parsing XML using XMLTextReader

I've been trying to parse the following XML file with the .NET XMLTextReader:

<?xml version="1.0"?><level1><level2><level3>Test</level3><level3>Test2</level3></level2><level2><level3>Test</level3><level3>Test2</level3><level3><level4>TEST<level5>TEST5</level5>TESTP</level4><level4>Test4</level4></level3></level2></level1>

However, the resulting output does not seem to make any sense:
Start Element: level1 @ 0
Start Element: level2 @ 1
Start Element: level3 @ 2
CDATA: Test @ 2
Start Element: level3 @ 2
CDATA: Test2 @ 2
CDATA: @ 1
Start Element: level2 @ 1
Start Element: level3 @ 2
CDATA: Test @ 2
Start Element: level3 @ 2
CDATA: Test2 @ 2
Start Element: level3 @ 2
Start Element: level4 @ 3
CDATA: TEST @ 4
CDATA: TEST5 @ 4
CDATA: TESTP @ 3
Start Element: level4 @ 3
CDATA: Test4 @ 3
CDATA: @ 2
CDATA: @ 1
CDATA: @ 0

Notice how "TEST" and "TESTP" are both contents of the "<level4>" tag, yet XMLTextReader thinks that they are at different levels. Furthermore, the "<level5>" tag is completely ignored as a "Start Element". What's going on? I thought mixing contents in an element was allowed.

Relevant Portion of my code:
public void Parse2()
{
while(MyXMLReader.Read())
{
if (MyXMLReader.IsStartElement())
{
Console.WriteLine("Start Element: " + MyXMLReader.Name + " @ " + MyXMLReader.Depth);
}
else
{
Console.WriteLine("CDATA: " + MyXMLReader.ReadString() + " @ " + MyXMLReader.Depth);
}
}
}

Thanks.
Nov 12 '05 #1
1 3737
Hang Cheng wrote:

Notice how "TEST" and "TESTP" are both contents of the "<level4>"
tag, yet XMLTextReader thinks that they are at different levels.
Furthermore, the "<level5>" tag is completely ignored as a "Start
Element". What's going on? I thought mixing contents in an element
was allowed.


Well, that's how you read it. To get better understanding of how
XmlReader reads the document, try something simpler:

while(MyXMLReader.Read()) {
Console.WriteLine("Node: {0}, name: {1}, value: {2}, depth: {3}",
MyXMLReader.NodeType, MyXMLReader.Name, MyXMLReader.Value,
MyXMLReader.Depth);
}

Your particular mistake is that you ignore еур behaviour of ReadString()
method when reading mixed content. As stated in MSDN:

"If positioned on an element, ReadString concatenates all text,
significant white space, white space, and CDATA section nodes together
and returns the concatenated data as the element content. It stops when
any markup is encountered. This could occur in a mixed content model, or
when an element end tag is read."

So, if you run the code in debugger, you can see that after calling
ReadString() on level4 node, reader gets positioned on level5 start tag.
But then Read() screws things moving the reader to "TEST5" text node.

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2

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

Similar topics

4
by: sunil | last post by:
I am creating a XML document which opens fine in IE. Implies MSXML thinks it is a well formed document. But when I try to load this document in VB.net using the following code Dim doc As New...
1
by: Jim P. | last post by:
I have a client server set of apps that can connect through socets and send data back and forth. I'm trying to get it to send XML messages back and both. Currently it works as string data. I...
3
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
0
by: Hang Cheng | last post by:
I've been trying to parse the following XML file with the .NET XMLTextReader <?xml...
2
by: Pushpendra | last post by:
I am storing the encrypted password in xml file which I have saved in unicode format. While I try to read the xml file and create a dataset from it, it shows me the following error ...
2
by: Mark | last post by:
Hi... We've been doing some basic performance testing comparing asp, asp.net, mono, and php. One of the basic tests is on simply parsing an xml document and streaming the result back to the...
2
by: nexus024 | last post by:
I am writing a program for one of my C++ classes at school and have gotten an error that I can't debug. What I am trying to do is write a C++ program that parses an XML file and displays to console...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
1
by: hd95 | last post by:
In a perfect world my xml feed source would produce perfect xml ..that is not the case I am parsing an XML feed that sometimes has ampersands and dashes in the content that messes up my parsing. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
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...
0
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...
0
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,...

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.