Reading Node Trees with XmlNodeReader
The XmlNodeReader provides a reader over a given DOM node subtree. It reads and returns nodes from the subtree, including entity reference nodes.
The XmlNodeReader provides the following functionality:
- Enforces the XML well-formedness rules.
- Expands default attributes and entities, if DTD information is present in the XmlDocument. For more information on getting default attribute information, see XmlReader.IsDefault Property.
To create an XmlNodeReader from an XmlDocument:
Dim doc As New XmlDocument() doc.Load("MyXml.xml") Dim nodereader As New XmlNodeReader(doc) While nodereader.Read() ' Read the XmlDocument as a stream of XML End While [C#] XmlDocument doc = new XmlDocument(); doc.Load("MyXml.xml"); XmlNodeReader nodereader = new XmlNodeReader (doc); while (nodereader.Read()) { // Read the XmlDocument as a stream of XML }
An XmlNodeReader can also be constructed with any XmlNode within the XmlDocument.
The following example moves to a particular node in the XmlDocument using the Select method and an XPath expression. It then creates an XmlNodeReader at that position. The XML input file, test.xml, contains the following data:
Imports System Imports System.Xml Public Class Test Public Shared Sub Main() Dim doc As New XmlDocument() doc.Load("test.xml") Dim child As XmlNode = doc.SelectSingleNode("/root/child") If Not (child Is Nothing) Then Dim nr As New XmlNodeReader(child) While nr.Read() Console.WriteLine(nr.Value) End While End If End Sub 'Main End Class 'Test [C#] using System; using System.Xml; public class Test { public static void Main() { XmlDocument doc = new XmlDocument(); doc.Load("test.xml"); XmlNode child = doc.SelectSingleNode("/root/child"); if (child != null) { XmlNodeReader nr = new XmlNodeReader(child ); while (nr.Read() ) Console.WriteLine( nr.Value ); } } }
See Also
Reading XML with the XmlReader | Reading XML Data with XmlTextReader | Validating XML with XmlValidatingReader | XmlReader Class | XmlReader Members | XmlNodeReader Class | XmlNodeReader Members | XmlTextReader Class | XmlTextReader Members | XmlValidatingReader Class | XmlValidatingReader