Export (0) Print
Expand All

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.

ExceptionCondition
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.

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.

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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft