473,387 Members | 1,545 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,387 software developers and data experts.

XmlTextReader reads only every other element

7
I have a very simple XML file and I'm trying to read every element, but as result, I get only every other element.

So instead of getting order_number, actions, failures, time and tester_name,
I only get order_number, failures and tester_name.
Why is this happening?

C# Function:
Expand|Select|Wrap|Line Numbers
  1. void ButtonLoadXMLClick(object sender, EventArgs e)
  2. {
  3.     textReader = new XmlTextReader(statisticsFolder + statisticsFile);
  4.  
  5.     while (textReader.Read())
  6.     {
  7.         if (textReader.NodeType == XmlNodeType.Element)
  8.         {
  9.             switch (textReader.Name.ToString())
  10.             {
  11.                 case "order_number":
  12.                     labelOrderNum.Text = textReader.ReadElementContentAsString();
  13.                     break;
  14.                 case "actions":
  15.                     labelActions.Text = textReader.ReadElementContentAsString();
  16.                     break;
  17.                 case "failures":
  18.                     labelFailures.Text = textReader.ReadElementContentAsString();
  19.                     break;
  20.                 case "time":
  21.                     labelTestTime.Text = textReader.ReadElementContentAsString();
  22.                     break;
  23.                 case "tester_name":
  24.                     labelTesterName.Text = textReader.ReadElementContentAsString();
  25.                     break;
  26.             }
  27.         }
  28.     }
  29. }
The XML document:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <root>
  3. <test_session>
  4.     <order_number>O_213762</order_number>
  5.     <actions>1246011</actions>
  6.     <failures>1536</failures>
  7.     <time>00:00:29</time>
  8.     <tester_name>-</tester_name>
  9. </test_session>
  10. </root>
Sep 11 '09 #1
2 2935
GaryTexmo
1,501 Expert 1GB
Maybe it's because you've got two reads there? I'm not 100% sure because I don't parse XML files this way, but you've got the read in your loop, then when you check the node you read again to assign the value. Maybe when you do this it advances to the next element, then the loop read advances to the next element again.

Maybe your loop should read the element into a variable and proceed from there? That's just my guess, anyways.

As an alternative, try using an XmlDocument and reading through the nodes?

Expand|Select|Wrap|Line Numbers
  1. XmlDocument doc = new XmlDocument();
  2. doc.LoadXml(theXmlString);
  3.  
  4. XmlNode root = doc.DocumentElement;
  5. foreach (XmlNode node in root.ChildNodes)
  6. {
  7.   // do something with node
  8.   // do something with node.ChildNodes if necessary
  9. }
Good luck!
Sep 11 '09 #2
alextj
7
Thank you Gary, your method seems to be more lucky than mine - I think I'm gonna be using it! ;)

Thanks!
Sep 13 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
1
by: Hang Cheng | last post by:
I've been trying to parse the following XML file with the .NET XMLTextReader: <?xml...
5
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...
5
by: Chris | last post by:
Hi, the docs say : "The Xml-document is not loaded into memory when using XmlTextReader, as opposed to using the DOM where the entire document is loaded in memory" but, when using...
1
by: Emsi | last post by:
Hello, how can I read values of child nodes with the XmlTextReader? File format: <root> <items> <item> <field1>value1</field1> <field2>value2</field2>
1
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
by: Michael | last post by:
Hi All, I have something going wrong with the XmlTextReader. I have a function that reads the following XML example. For some reason the code is only getting two of the 3 Values elements. Here is...
4
by: CodeRazor | last post by:
I am trying to use an XmlTextReader to retrieve data. I need to use an XmlTextReader because it is faster than using an XmlDocument. I have found an inelegant way of retrieving each item's title...
13
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...

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.