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.
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>
/// 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
- On the Debug menu, click
Start to run the application.
- Notice in the Output window that the n1 node from the input XML is not skipped.
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed at the beginning of this article.
Steps to Reproduce the Behavior
- 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>
/// 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
- On the Debug menu, click
Start to run the application.
- 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.
For more information about the
XmlValidatingReader class and the
XmlTextReader class, visit the following Microsoft Developer Network (MSDN) Web
sites:
For additional information, click the
following article numbers to view the articles in the Microsoft Knowledge Base:
307548
(http://support.microsoft.com/kb/307548/
)
HOW TO: Read XML from a File by Using Visual C# .NET
301225
(http://support.microsoft.com/kb/301225/
)
HOW TO: Read XML from a File by Using Visual Basic .NET