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



The information in this article applies to:

  • Microsoft XML 2.0
  • Microsoft .NET Framework 1.0
  • Microsoft XML Classes (included with the .NET Framework 1.1)
  • Microsoft XML Classes (included with the .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:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C# Projects or Visual Basic Projects under Project Types, and then click Console Application under Templates.
  4. 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>
    	/// Summary description for Class1.
    	/// </summary>
    	class Class1
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			//
    			// TODO: Add code to start application here
    			//
    			StringReader sReader = new StringReader("<root><n1>1</n1><n2>2</n2></root>");
    			XmlTextReader r = new XmlTextReader(sReader);
    			r.MoveToContent();
    			r.Read();
    
    			Console.WriteLine("Node type and name after First Read = " +
    				r.NodeType + " : " + r.Name);
    			Console.WriteLine("ReadString result when positioned on " + 
    				r.Name + " = " + r.ReadString());
    			Console.WriteLine("Node type and name after ReadString = " + 
    				r.NodeType + " : " + r.Name);
    			r.Read();
    			Console.WriteLine("Node type and name after Second Read = " + 
    				r.NodeType + " : " + 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("<root><n1>1</n1><n2>2</n2></root>"))
            r.MoveToContent()
            r.Read()
    
            Console.WriteLine("Node type and name after First Read = " & _
                r.NodeType & " : " & r.Name)
            Console.WriteLine("ReadString result when positioned on " & _
                r.Name & " = " & r.ReadString())
            Console.WriteLine("Node type and name after ReadString = " & _
                r.NodeType & " : " & r.Name)
            r.Read()
            Console.WriteLine("Node type and name after Second Read = " & _
                r.NodeType & " : " & r.Name)
            Console.ReadLine()
        End Sub
    
    End Module
    
  5. On the Debug menu, click Start to run the application.
  6. Notice in the Output window that the n1 node from the input XML is not skipped.

STATUS

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

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New and then click Project.
  3. Click Visual C# Projects or Visual Basic Projects under Project Types, and then click Console Application under Templates.
  4. 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>
    	/// Summary description for Class1.
    	/// </summary>
    	class Class1
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			//
    			// TODO: Add code to start application here
    			//
    			StringReader sReader = new StringReader("<root><n1>1</n1><n2>2</n2></root>");
    			XmlTextReader r = new XmlTextReader(sReader);
    				r.Read();
    
    			Console.WriteLine("Node type and name after First Read = " +
    				r.NodeType + " : " + r.Name);
    			Console.WriteLine("ReadString result when positioned on " + 
    				r.Name + " = " + r.ReadString());
    			Console.WriteLine("Node type and name after ReadString = " + 
    				r.NodeType + " : " + r.Name);
    			r.Read();
    			Console.WriteLine("Node type and name after Second Read = " + 
    				r.NodeType + " : " + 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("<root><n1>1</n1><n2>2</n2></root>"))
            r.Read()
    
            Console.WriteLine("Node type and name after First Read = " & _
                r.NodeType & " : " & r.Name)
            Console.WriteLine("ReadString result when positioned on " & _
                r.Name & " = " & r.ReadString())
            Console.WriteLine("Node type and name after ReadString = " & _
                r.NodeType & " : " & r.Name)
            r.Read()
            Console.WriteLine("Node type and name after Second Read = " & _
                r.NodeType & " : " & r.Name)
            Console.ReadLine()
        End Sub
    
    End Module
    
  5. On the Debug menu, click Start to run the application.
  6. Notice in the Output window that the n1 node from the input XML is skipped.
Note This behavior is also true for the XmlValidatingReader class.

REFERENCES


For more information about the XmlValidatingReader class and the XmlTextReader class, visit the following Microsoft Developer Network (MSDN) Web sites:
For more information about how to read XML from a file, visit the following Microsoft Web site: For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

307548 HOW TO: Read XML from a File by Using Visual C# .NET

301225 HOW TO: Read XML from a File by Using Visual Basic .NET


Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbXML kbbug KB823882 kbAudDeveloper