Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
XmlDocument Class
 ReadNode Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
XmlDocument..::.ReadNode Method

Creates an XmlNode object based on the information in the XmlReader. The reader must be positioned on a node or attribute.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
Visual Basic (Declaration)
<PermissionSetAttribute(SecurityAction.InheritanceDemand, Name := "FullTrust")> _
Public Overridable Function ReadNode ( _
    reader As XmlReader _
) As XmlNode
Visual Basic (Usage)
Dim instance As XmlDocument
Dim reader As XmlReader
Dim returnValue As XmlNode

returnValue = instance.ReadNode(reader)
C#
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public virtual XmlNode ReadNode(
    XmlReader reader
)
Visual C++
[PermissionSetAttribute(SecurityAction::InheritanceDemand, Name = L"FullTrust")]
public:
virtual XmlNode^ ReadNode(
    XmlReader^ reader
)
JScript
public function ReadNode(
    reader : XmlReader
) : XmlNode

Parameters

reader
Type: System.Xml..::.XmlReader
The XML source

Return Value

Type: System.Xml..::.XmlNode
The new XmlNode or nullNothingnullptra null reference (Nothing in Visual Basic) if no more nodes exist.
ExceptionCondition
NullReferenceException

The reader is positioned on a node type that does not translate to a valid DOM node (for example, EndElement or EndEntity).

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.

Notes to Inheritors:

This method has an inheritance demand. Full trust is required to override the ReadNode method. See Inheritance Demands for more information.

This method is a Microsoft extension to the Document Object Model (DOM).

The following example uses ReadNode to create a new node and then inserts the new node into the document.

Visual Basic
Option Explicit
Option Strict

Imports System
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 'Main 
End Class 'Sample
C#
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);

  }
}
Visual C++
#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 );
}

CPP_OLD
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    //Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<bookstore><book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book></bookstore>");

    //Create a reader.
    XmlTextReader* reader = new XmlTextReader(S"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(S"Display the modified XML...");
    doc->Save(Console::Out);
}

The example uses the file, cd.xml, as input.

None
<!-- sample CD -->
<cd genre='alternative'>
  <title>Americana</title>
  <artist>Offspring</artist>
</cd>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker