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:
- 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.
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
- 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.
REFERENCES
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://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
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