
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
836
|
Chapter 15: XML
while (reader.Read( ))
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
}
}
}
Discussion
Using the XmlReader.Create method with a URI is a quick way to access XML docu-
ments that are stored remotely without writing all of the connectivity code. This uses
an instance of the
XmlUrlResolver class to check the URI passed in and then opens a
stream to the XML document indicated by the URI. To specify settings on the
reader, there is a second overload of
Create that also takes an XmlReaderSettings
instance to facilitate this.
The sample.xml file being referenced in this code is set up in a virtual directory
named xml on the local system. The code retrieves the sample.xml file from the web
server and displays all of the elements in the XML.
sample.xml contains the following XML data:
<?xml version='1.0'?>
<!-- My sample XML -->
<?pi myProcessingInstruction?>
<Root>
<Node1 nodeId='1'>First Node</Node1>
<Node2 nodeId='2'>Second Node</Node2>
<Node3 nodeId='3'>Third Node</Node3>
<Node4><![CDATA[<>\&']]></Node4>
</Root>
See Also
See the “XmlReader Class” topic in the MSDN documentation.
15.3 Querying the Contents of an XML Document
Problem
You have a large and complex XML document and you need to find various pieces of
information, such ...