System.Xml.XmlDocument.ReadNode Method

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

Syntax

public virtual XmlNode ReadNode (XmlReader reader)

Parameters

reader
The XML source

Returns

The new XmlNode or null if no more nodes exist.

Remarks

Reads one XmlNode from the given reader and positions the reader on the next node. This method creates the type of XmlNode matching the XmlNode.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:

Example

 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.

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0