Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 584839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:56:24+00:00 2026-05-13T14:56:24+00:00

It seems every time I use an XMLReader, I end up with a bunch

  • 0

It seems every time I use an XMLReader, I end up with a bunch of trial and error trying to figure out what I’m about to read versus what I’m reading versus what I just read. I always figure it out in the end, but I still, after using it numerous times, don’t seem to have a firm grasp of what an XMLReader is actually doing when I call the various functions. For example, when I call Read the first time, if it reads an element start tag, is it now at the end of the element tag, or ready to begin reading the element’s attributes? Does it know the values of the attributes yet if I call GetAttribute? What will happen if I call ReadStartElement at this point? Will it finish reading the start element, or look for the next one, skipping all the attributes? What if I want to read a number of elements — what’s the best way to try to read the next element and determine what its name is. Will Read followed by IsStartElement work, or will IsStartElement be returning information about the node following the element I just read?

As you can see I really am lacking an understanding of where an XMLReader is at during the various phases of its reading and how its state is affected by various read functions. Is there some simple pattern that I’ve simply failed to notice?

Here’s another example of the problem (taken from the responses):

string input = "<machine code=\"01\">The Terminator" +
   "<part code=\"01a\">Right Arm</part>" +
   "<part code=\"02\">Left Arm</part>" +
   "<part code=\"03\">Big Toe</part>" +
   "</machine>";

using (System.IO.StringReader sr = new System.IO.StringReader(input))
{
   using (XmlTextReader reader = new XmlTextReader(sr))
   {
      reader.WhitespaceHandling = WhitespaceHandling.None;
      reader.MoveToContent();

      while(reader.Read())
      {
         if (reader.Name.Equals("machine") && (reader.NodeType == XmlNodeType.Element))
         {
            Console.Write("Machine code {0}: ", reader.GetAttribute("code"));
            Console.WriteLine(reader.ReadElementString("machine"));
         }
         if(reader.Name.Equals("part") && (reader.NodeType == XmlNodeType.Element))
         {
            Console.Write("Part code {0}: ", reader.GetAttribute("code"));
            Console.WriteLine(reader.ReadElementString("part"));
         }
      }
   }
}

First problem, the machine node is skipped completely. MoveToContent seems to move to the content of the machine element causing it to never be parsed. Furthermore, if you skip MoveToContent, you get an error: “‘Element’ is an invalid XmlNodeType.” trying to ReadElementString, which I can’t quite explain.

Next problem is, while reading the first part element, ReadElementString seems to position the reader at the beginning of the next part element after reading. This causes the reader.Read at the beginning of the next loop to skip over the next part element jumping right to the last part element. So the final output of this code is:

Part code 01a: Right Arm

Part code 03: Big Toe

This is a prime example of the confusign behavior of XMLReader that I’m trying to understand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:56:24+00:00Added an answer on May 13, 2026 at 2:56 pm

    My latest solution (which works for my current case) is to stick with Read(), IsStartElement(name) and GetAttribute(name) in implementing a state machine.

    using (System.Xml.XmlReader xr = System.Xml.XmlTextReader.Create(stm))
    {
       employeeSchedules = new Dictionary<string, EmployeeSchedule>();
       EmployeeSchedule emp = null;
       WeekSchedule sch = null;
       TimeRanges ranges = null;
       TimeRange range = null;
       while (xr.Read())
       {
          if (xr.IsStartElement("Employee"))
          {
             emp = new EmployeeSchedule();
             employeeSchedules.Add(xr.GetAttribute("Name"), emp);
          }
          else if (xr.IsStartElement("Unavailable"))
          {
             sch = new WeekSchedule();
             emp.unavailable = sch;
          }
          else if (xr.IsStartElement("Scheduled"))
          {
             sch = new WeekSchedule();
             emp.scheduled = sch;
          }
          else if (xr.IsStartElement("DaySchedule"))
          {
             ranges = new TimeRanges();
             sch.daySchedule[int.Parse(xr.GetAttribute("DayNumber"))] = ranges;
             ranges.Color = ParseColor(xr.GetAttribute("Color"));
             ranges.FillStyle = (System.Drawing.Drawing2D.HatchStyle)
                System.Enum.Parse(typeof(System.Drawing.Drawing2D.HatchStyle),
                xr.GetAttribute("Pattern"));
          }
          else if (xr.IsStartElement("TimeRange"))
          {
             range = new TimeRange(
                System.Xml.XmlConvert.ToDateTime(xr.GetAttribute("Start"),
                System.Xml.XmlDateTimeSerializationMode.Unspecified),
                new TimeSpan((long)(System.Xml.XmlConvert.ToDouble(xr.GetAttribute("Length")) * TimeSpan.TicksPerHour)));
             ranges.Add(range);
          }
       }
       xr.Close();
    }
    

    After Read, IsStartElement will return true if you just read a start element (optinally checking the name of the element read), and you can access all the attributes of that element immediately. If all you need to read is elements and attributes, this is pretty straightforward.

    Edit
    The new example posted in the question poses some other challenges. The correct way to read that XML seems to be like this:

    using (System.IO.StringReader sr = new System.IO.StringReader(input))
    {
       using (XmlTextReader reader = new XmlTextReader(sr))
       {
          reader.WhitespaceHandling = WhitespaceHandling.None;
    
          while(reader.Read())
          {
             if (reader.Name.Equals("machine") && (reader.NodeType == XmlNodeType.Element))
             {
                Console.Write("Machine code {0}: ", reader.GetAttribute("code"));
                Console.WriteLine(reader.ReadString());
             }
             if(reader.Name.Equals("part") && (reader.NodeType == XmlNodeType.Element))
             {
                Console.Write("Part code {0}: ", reader.GetAttribute("code"));
                Console.WriteLine(reader.ReadString());
             }
          }
       }
    }
    

    You have to use ReadString instead of ReadElementString in order to avoid reading the end element and skipping into the beginning of the next element (let the following Read() skip over the end element so it doesn’t skip over the next start element). Still this seems somewhat confusing and potentially unreliable, but it works for this case.

    After some additional thought, my opinion is that XMLReader is just too confusing if you use any methods to read content other than the Read method. I think it’s much simpler if you confine yourself to the Read method to read from the XML stream. Here’s how it would work with the new example (once again, it seems IsStartElement, GetAttribute and Read are the key methods, and you end up with a state machine):

    while(reader.Read())
    {
       if (reader.IsStartElement("machine"))
       {
          Console.Write("Machine code {0}: ", reader.GetAttribute("code"));
       }
       if(reader.IsStartElement("part"))
       {
          Console.Write("Part code {0}: ", reader.GetAttribute("code"));
       }
       if (reader.NodeType == XmlNodeType.Text)
       {
          Console.WriteLine(reader.Value);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Like this: ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream w =… May 15, 2026 at 4:56 am
  • Editorial Team
    Editorial Team added an answer If I move my application to another PC I won't… May 15, 2026 at 4:56 am
  • Editorial Team
    Editorial Team added an answer Unfortunately, no, it is not possible to do that with… May 15, 2026 at 4:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.