Microsoft KB Archive/823882

= BUG: ReadString Method of the XMLTextReader Class Skips a Node When It Is Positioned on the Root Node or on the DocumentElement Node =

Article ID: 823882

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft XML Parser 2.0
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



SYMPTOMS
The ReadString method of the XmlTextReader class and the XmlValidatingReader class skips an extra node when the method is positioned on the documentElement node or the root node of the XML document in the Microsoft .NET Framework 1.1.



WORKAROUND
To work around this problem, introduce a call to the MoveToContent method of the XmlTextReader class before you run the first Read method of the XmlTextReader class. When you do this, the ReadString method skips the documentElement in an XML document, but does not skip any additional nodes.

To do this, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Click Visual C# Projects or Visual Basic Projects under Project Types, and then click Console Application under Templates.  Replace the code in the Class1.cs file with the following code:

Visual C# .NET Code
using System; using System.Xml; using System.IO;

namespace _823882CSharp {   ///     /// Summary description for Class1. ///    class Class1 {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           //            // TODO: Add code to start application here //           StringReader sReader = new StringReader(&quot; 12 &quot;); XmlTextReader r = new XmlTextReader(sReader); r.MoveToContent; r.Read;

Console.WriteLine(&quot;Node type and name after First Read = &quot; +               r.NodeType + &quot; : &quot; + r.Name); Console.WriteLine(&quot;ReadString result when positioned on &quot; +                r.Name + &quot; = &quot; + r.ReadString); Console.WriteLine(&quot;Node type and name after ReadString = &quot; +                r.NodeType + &quot; : &quot; + r.Name); r.Read; Console.WriteLine(&quot;Node type and name after Second Read = &quot; +                r.NodeType + &quot; : &quot; + r.Name); Console.ReadLine; }   } }  Replace the code in the Module1.vb file with the following code:

Visual Basic .NET Code
Imports System Imports System.Xml Imports System.IO Module Module1

Sub Main Dim r As New _ Xml.XmlTextReader(New System.IO.StringReader(&quot; 12</n2> &quot;)) r.MoveToContent r.Read

Console.WriteLine(&quot;Node type and name after First Read = &quot; & _           r.NodeType & &quot; : &quot; & r.Name) Console.WriteLine(&quot;ReadString result when positioned on &quot; & _           r.Name & &quot; = &quot; & r.ReadString) Console.WriteLine(&quot;Node type and name after ReadString = &quot; & _           r.NodeType & &quot; : &quot; & r.Name) r.Read Console.WriteLine(&quot;Node type and name after Second Read = &quot; & _           r.NodeType & &quot; : &quot; & r.Name) Console.ReadLine End Sub

End Module </li> On the Debug menu, click Start to run the application.</li> Notice in the Output window that the n1 node from the input XML is not skipped.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Start Microsoft Visual Studio .NET.</li> On the File menu, point to New and then click Project.</li> Click Visual C# Projects or Visual Basic Projects under Project Types, and then click Console Application under Templates.</li>  Replace the code in the Class1.cs file with the following code:

Visual C# .NET code
using System; using System.Xml; using System.IO;

namespace _823882CSharp {   ///     /// Summary description for Class1. ///    class Class1 {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           //            // TODO: Add code to start application here //           StringReader sReader = new StringReader(&quot; 1</n1>2</n2> &quot;); XmlTextReader r = new XmlTextReader(sReader); r.Read;

Console.WriteLine(&quot;Node type and name after First Read = &quot; +               r.NodeType + &quot; : &quot; + r.Name); Console.WriteLine(&quot;ReadString result when positioned on &quot; +                r.Name + &quot; = &quot; + r.ReadString); Console.WriteLine(&quot;Node type and name after ReadString = &quot; +                r.NodeType + &quot; : &quot; + r.Name); r.Read; Console.WriteLine(&quot;Node type and name after Second Read = &quot; +                r.NodeType + &quot; : &quot; + r.Name); Console.ReadLine; }   } }  Replace the code in the Module1.vb file with the following code:

Visual Basic .NET code
Imports System Imports System.Xml Imports System.IO Module Module1

Sub Main Dim r As New _ Xml.XmlTextReader(New System.IO.StringReader(&quot; 1</n1>2</n2> &quot;)) r.Read

Console.WriteLine(&quot;Node type and name after First Read = &quot; & _           r.NodeType & &quot; : &quot; & r.Name) Console.WriteLine(&quot;ReadString result when positioned on &quot; & _           r.Name & &quot; = &quot; & r.ReadString) Console.WriteLine(&quot;Node type and name after ReadString = &quot; & _           r.NodeType & &quot; : &quot; & r.Name) r.Read Console.WriteLine(&quot;Node type and name after Second Read = &quot; & _           r.NodeType & &quot; : &quot; & r.Name) Console.ReadLine End Sub

End Module </li> On the Debug menu, click Start to run the application.</li> Notice in the Output window that the n1 node from the input XML is skipped.</li></ol>

Note This behavior is also true for the XmlValidatingReader class.

<div class="references_section">