XmlDocument.ReadNode(XmlReader) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
public:
virtual System::Xml::XmlNode ^ ReadNode(System::Xml::XmlReader ^ reader);
public virtual System.Xml.XmlNode ReadNode (System.Xml.XmlReader reader);
public virtual System.Xml.XmlNode? ReadNode (System.Xml.XmlReader reader);
abstract member ReadNode : System.Xml.XmlReader -> System.Xml.XmlNode
override this.ReadNode : System.Xml.XmlReader -> System.Xml.XmlNode
Public Overridable Function ReadNode (reader As XmlReader) As XmlNode
- reader
- XmlReader
The XML source.
The new XmlNode
or null
if no more nodes exist.
The reader is positioned on a node type that does not translate to a valid DOM node (for example, EndElement or EndEntity).
The following example uses ReadNode
to create a new node and then inserts the new node into the document.
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
//Create the XmlDocument.
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<bookstore><book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book></bookstore>" );
//Create a reader.
XmlTextReader^ reader = gcnew XmlTextReader( "cd.xml" );
reader->MoveToContent(); //Move to the cd element node.
//Create a node representing the cd element node.
XmlNode^ cd = doc->ReadNode( reader );
//Insert the new node into the document.
doc->DocumentElement->AppendChild( cd );
Console::WriteLine( "Display the modified XML..." );
doc->Save( Console::Out );
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<bookstore>" +
"<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>" +
"</bookstore>");
//Create a reader.
XmlTextReader reader = new XmlTextReader("cd.xml");
reader.MoveToContent(); //Move to the cd element node.
//Create a node representing the cd element node.
XmlNode cd = doc.ReadNode(reader);
//Insert the new node into the document.
doc.DocumentElement.AppendChild(cd);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
'Create the XmlDocument.
Dim doc As New XmlDocument()
doc.LoadXml("<bookstore>" & _
"<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>" & _
"</bookstore>")
'Create a reader.
Dim reader As New XmlTextReader("cd.xml")
reader.MoveToContent() 'Move to the cd element node.
'Create a node representing the cd element node.
Dim cd As XmlNode = doc.ReadNode(reader)
'Insert the new node into the document.
doc.DocumentElement.AppendChild(cd)
Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub
End Class
The example uses the file, cd.xml
, as input.
<!-- sample CD -->
<cd genre='alternative'>
<title>Americana</title>
<artist>Offspring</artist>
</cd>
Reads one XmlNode
from the given reader and positions the reader on the next node. This method creates the type of XmlNode
matching the NodeType on which the reader is currently positioned. (If the reader is in the initial state, ReadNode
advances the reader to the first node and then operates on that node.)
If the reader is positioned on the start of an element, ReadNode
reads all the attributes and any child nodes, up to and including the end tag of the current node. The XmlNode
returned contains the sub-tree representing everything read. The reader is positioned immediately after the end tag.
ReadNode
can also read attributes, but in this case it does not advance the reader to the next attribute. This allows you to write the following C# code:
XmlDocument doc = new XmlDocument();
while (reader.MoveToNextAttribute())
{
XmlNode a = doc.ReadNode(reader);
// Do some more processing.
}
ReadNode
does consume the attribute value though, which means after calling ReadNode
on an attribute, XmlReader.ReadAttributeValue returns false
.
This method has an inheritance demand. Full trust is required to override the ReadNode
method.
This method is a Microsoft extension to the Document Object Model (DOM).
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: