Microsoft Knowledge Base Email Alertz

KBAlertz.com: (823882) - 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.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 823882 - Last Review: November 26, 2007 - Revision: 1.5

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

On This Page

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:
http://msdn2.microsoft.com/en-us/library/system.xml.xmlvalidatingreader(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.xml.xmlvalidatingreader(vs.71).aspx)
http://msdn2.microsoft.com/en-us/library/system.xml.xmltextreader(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.xml.xmltextreader(vs.71).aspx)
For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
307548  (http://kbalertz.com/Feedback.aspx?kbNumber=307548/ ) HOW TO: Read XML from a File by Using Visual C# .NET
301225   (http://kbalertz.com/Feedback.aspx?kbNumber=301225/ ) HOW TO: Read XML from a File by Using Visual Basic .NET

APPLIES TO
  • Microsoft XML Parser 2.0
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
Keywords: 
kbxml kbbug KB823882
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please