Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   |   Class Browser
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

How Do I...Use an XmlNodeReader?

This sample illustrates how to create and use an XmlNodeReader. An XmlNodeReader is a reader that provides fast, non-cached, forward-only access to XML data in an XmlNode. It has the ability to read an entire XML DOM tree or read from just a subtree. However, the XmlNodeReader does not support Document Type Definition (DTD) or schema validation, and therefore does not validate the XML it is reading.

This sample loads the books.xml into an XmlDocument, and then uses an XmlNodeReader to select a node and display the node value to the screen.

C# XmlNodeReader.exe
View Source

[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Xml\XmlNodeReader\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]

After creating and loading an XmlDocument with the books.xml file, the sample creates an XmlNodeReader that selects a single node from the XmlDocument and prints the node data to the screen. The sample then creates another XmlNodeReader that selects a different node to output to the screen.

		
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(document);
...
Console.WriteLine("Create an XmlNodeReader to show the third book ...");
using (XmlNodeReader xmlNodeReader = 
    new XmlNodeReader(xmlDocument.SelectSingleNode("bookstore/book[3]")))
{
...
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
    {
    writer.WriteNode(xmlNodeReader, true);
    }
}
C#

Summary

  1. XmlNodeReader is a fast, non-cached, forward-only reader to access XML data in an XmlNode.
  2. Because an XmlNodeReader can be constructed with any XmlNode within the XmlDocument, XmlNodeReader provides a reader that reads only the subtree of a given node.




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2008  Mindcracker LLC. All Rights Reserved