XmlTextReader Constructor (String)

 

Initializes a new instance of the XmlTextReader class with the specified file.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public XmlTextReader(
	string url
)

Parameters

url
Type: System.String

The URL for the file containing the XML data. The BaseURI is set to this value.

Exception Condition
FileNotFoundException

The specified file cannot be found.

DirectoryNotFoundException

Part of the filename or directory cannot be found.

InvalidOperationException

url is an empty string.

WebException

The remote filename cannot be resolved.

-or-

An error occurred while processing the request.

UriFormatException

url is not a valid URI.

System_CAPS_noteNote

Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

If the file is located on a resource that requires access credentials, use the XmlResolver property to specify the necessary credentials.

System_CAPS_noteNote

In version 1.1 of the .NET Framework, partially trusted code cannot set the XmlResolver property. The workaround is to create an XmlUrlResolver with the necessary credentials, pass the URI to the XmlUrlResolver.GetEntity method, and then construct the XmlTextReader using the resulting Stream object. The workaround is described in the following C# code.

// Create a resolver with the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
NetworkCredential nc = new NetworkCredential(SecurelyStoredUserName, SecurelyStoredPassword, SecurelyStoredDomain);
resolver.Credentials = nc; 
// Get a Stream object containing the XML file.
Uri myUri = new Uri ("http://myServer/data/books.xml");
Stream s=(Stream)resolver.GetEntity(myUri, null, typeof(Stream));
// Construct a reader using the Stream object.
XmlTextReader reader = new XmlTextReader(s);

The following example reads an XML file and displays each of the nodes.

using System;
using System.IO;
using System.Xml;

public class Sample {

  private const String filename = "items.xml";

  public static void Main() {

     XmlTextReader reader = null;

     try {

        // Load the reader with the data file and ignore all white space nodes.         
        reader = new XmlTextReader(filename);
        reader.WhitespaceHandling = WhitespaceHandling.None;

        // Parse the file and display each of the nodes.
        while (reader.Read()) {
           switch (reader.NodeType) {
             case XmlNodeType.Element:
               Console.Write("<{0}>", reader.Name);
               break;
             case XmlNodeType.Text:
               Console.Write(reader.Value);
               break;
             case XmlNodeType.CDATA:
               Console.Write("<![CDATA[{0}]]>", reader.Value);
               break;
             case XmlNodeType.ProcessingInstruction:
               Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
               break;
             case XmlNodeType.Comment:
               Console.Write("<!--{0}-->", reader.Value);
               break;
             case XmlNodeType.XmlDeclaration:
               Console.Write("<?xml version='1.0'?>");
               break;
             case XmlNodeType.Document:
               break;
             case XmlNodeType.DocumentType:
               Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
               break;
             case XmlNodeType.EntityReference:
               Console.Write(reader.Name);
               break;
             case XmlNodeType.EndElement:
               Console.Write("</{0}>", reader.Name);
               break;
           }       
        }           
     }

     finally {
        if (reader!=null)
          reader.Close();
     }
  }
} // End class

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


<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
  <Item>Test with an entity: &number;</Item>
  <Item>test with a child element <more/> stuff</Item>
  <Item>test with a CDATA section <![CDATA[<456>]]> def</Item>
  <Item>Test with an char entity: &#65;</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

.NET Framework
Available since 1.1
Return to top
Show: