XmlTextReader XmlTextReader XmlTextReader XmlTextReader Constructors

Definition

Overloads

XmlTextReader() XmlTextReader() XmlTextReader()

Initializes a new instance of the XmlTextReader.

XmlTextReader(String, XmlNodeType, XmlParserContext) XmlTextReader(String, XmlNodeType, XmlParserContext) XmlTextReader(String, XmlNodeType, XmlParserContext) XmlTextReader(String, XmlNodeType, XmlParserContext)

Initializes a new instance of the XmlTextReader class with the specified string, XmlNodeType, and XmlParserContext.

XmlTextReader(String, TextReader, XmlNameTable) XmlTextReader(String, TextReader, XmlNameTable) XmlTextReader(String, TextReader, XmlNameTable) XmlTextReader(String, TextReader, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified URL, TextReader and XmlNameTable.

XmlTextReader(String, Stream, XmlNameTable) XmlTextReader(String, Stream, XmlNameTable) XmlTextReader(String, Stream, XmlNameTable) XmlTextReader(String, Stream, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified URL, stream and XmlNameTable.

XmlTextReader(Stream, XmlNodeType, XmlParserContext) XmlTextReader(Stream, XmlNodeType, XmlParserContext) XmlTextReader(Stream, XmlNodeType, XmlParserContext) XmlTextReader(Stream, XmlNodeType, XmlParserContext)

Initializes a new instance of the XmlTextReader class with the specified stream, XmlNodeType, and XmlParserContext.

XmlTextReader(String, TextReader) XmlTextReader(String, TextReader) XmlTextReader(String, TextReader) XmlTextReader(String, TextReader)

Initializes a new instance of the XmlTextReader class with the specified URL and TextReader.

XmlTextReader(String, Stream) XmlTextReader(String, Stream) XmlTextReader(String, Stream) XmlTextReader(String, Stream)

Initializes a new instance of the XmlTextReader class with the specified URL and stream.

XmlTextReader(String, XmlNameTable) XmlTextReader(String, XmlNameTable) XmlTextReader(String, XmlNameTable) XmlTextReader(String, XmlNameTable)

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

XmlTextReader(Stream, XmlNameTable) XmlTextReader(Stream, XmlNameTable) XmlTextReader(Stream, XmlNameTable) XmlTextReader(Stream, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified stream and XmlNameTable.

XmlTextReader(XmlNameTable) XmlTextReader(XmlNameTable) XmlTextReader(XmlNameTable) XmlTextReader(XmlNameTable)

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

XmlTextReader(String) XmlTextReader(String) XmlTextReader(String) XmlTextReader(String)

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

XmlTextReader(TextReader) XmlTextReader(TextReader) XmlTextReader(TextReader) XmlTextReader(TextReader)

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

XmlTextReader(Stream) XmlTextReader(Stream) XmlTextReader(Stream) XmlTextReader(Stream)

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

XmlTextReader(TextReader, XmlNameTable) XmlTextReader(TextReader, XmlNameTable) XmlTextReader(TextReader, XmlNameTable) XmlTextReader(TextReader, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified TextReader and XmlNameTable.

Remarks

Note

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.

XmlTextReader() XmlTextReader() XmlTextReader()

Initializes a new instance of the XmlTextReader.

protected:
 XmlTextReader();
protected XmlTextReader ();
Protected Sub New ()
See also

XmlTextReader(String, XmlNodeType, XmlParserContext) XmlTextReader(String, XmlNodeType, XmlParserContext) XmlTextReader(String, XmlNodeType, XmlParserContext) XmlTextReader(String, XmlNodeType, XmlParserContext)

Initializes a new instance of the XmlTextReader class with the specified string, XmlNodeType, and XmlParserContext.

public:
 XmlTextReader(System::String ^ xmlFragment, System::Xml::XmlNodeType fragType, System::Xml::XmlParserContext ^ context);
public XmlTextReader (string xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext context);
new System.Xml.XmlTextReader : string * System.Xml.XmlNodeType * System.Xml.XmlParserContext -> System.Xml.XmlTextReader
Public Sub New (xmlFragment As String, fragType As XmlNodeType, context As XmlParserContext)

Parameters

xmlFragment
String String String String

The string containing the XML fragment to parse.

fragType
XmlNodeType XmlNodeType XmlNodeType XmlNodeType

The XmlNodeType of the XML fragment. This also determines what the fragment string can contain. (See table below.)

context
XmlParserContext XmlParserContext XmlParserContext XmlParserContext

The XmlParserContext in which the xmlFragment is to be parsed. This includes the XmlNameTable to use, encoding, namespace scope, the current xml:lang, and the xml:space scope.

Exceptions

fragType is not an Element, Attribute, or DocumentXmlNodeType.

Examples

The following example parses an XML fragment. It uses the XmlParserContext and its XmlNamespaceManager to handle namespace resolution.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XML fragment to be parsed.
   String^ xmlFrag = "<book> <title>Pride And Prejudice</title> <bk:genre>novel</bk:genre> </book>";
   
   // Create the XmlNamespaceManager.
   NameTable^ nt = gcnew NameTable;
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( nt );
   nsmgr->AddNamespace( "bk", "urn:sample" );
   
   // Create the XmlParserContext.
   XmlParserContext^ context = gcnew XmlParserContext( nullptr,nsmgr,nullptr,XmlSpace::None );
   
   // Create the reader. 
   XmlTextReader^ reader = gcnew XmlTextReader( xmlFrag,XmlNodeType::Element,context );
   
   // Parse the XML.  If they exist, display the prefix and  
   // namespace URI of each element.
   while ( reader->Read() )
   {
      if ( reader->IsStartElement() )
      {
         if ( reader->Prefix == String::Empty )
                  Console::WriteLine( "< {0}>", reader->LocalName );
         else
         {
            Console::Write( "< {0}: {1}>", reader->Prefix, reader->LocalName );
            Console::WriteLine( " The namespace URI is {0}", reader->NamespaceURI );
         }
      }
   }

   
   // Close the reader.
   reader->Close();
}

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

public class Sample 
{
  public static void Main()
  {

    //Create the XML fragment to be parsed.
    string xmlFrag ="<book> " +
                    "<title>Pride And Prejudice</title>" +
                    "<bk:genre>novel</bk:genre>" +
                    "</book>"; 

    //Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
    nsmgr.AddNamespace("bk", "urn:sample");

    //Create the XmlParserContext.
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

    //Create the reader. 
    XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
  
    //Parse the XML.  If they exist, display the prefix and  
    //namespace URI of each element.
    while (reader.Read()){
      if (reader.IsStartElement()){
        if (reader.Prefix==String.Empty)
           Console.WriteLine("<{0}>", reader.LocalName);
        else{
            Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName);
            Console.WriteLine(" The namespace URI is " + reader.NamespaceURI);
        }
      }
    }
  
    //Close the reader.
    reader.Close();     
  
  }
} // End class
Imports System
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main()

    'Create the XML fragment to be parsed.
    Dim xmlFrag as string ="<book> " & _
                           "<title>Pride And Prejudice</title>" & _
                           "<bk:genre>novel</bk:genre>" & _
                           "</book>" 

    'Create the XmlNamespaceManager.
    Dim nt as NameTable = new NameTable()
    Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(nt)
    nsmgr.AddNamespace("bk", "urn:sample")

    'Create the XmlParserContext.
    Dim context as XmlParserContext = new XmlParserContext(nothing, nsmgr, nothing, XmlSpace.None)

    'Create the reader. 
    Dim reader as XmlTextReader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)
  
    'Parse the XML.  If they exist, display the prefix and  
    'namespace URI of each element.
    while (reader.Read())
      if (reader.IsStartElement())
        if (reader.Prefix=String.Empty)
           Console.WriteLine("<{0}>", reader.LocalName)
        else
            Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName)
            Console.WriteLine(" The namespace URI is " + reader.NamespaceURI)
        end if 
      end if
    end while
  
    'Close the reader.
    reader.Close()     
  
  end sub
end class

Remarks

Note

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.

This constructor parses the given string as a fragment of XML. If the XML fragment is an element or attribute, you can bypass the root level rules for well-formed XML documents. This constructor can handle strings returned from ReadInnerXml.

The following table lists valid values for fragType and how the reader parses each of the different node types.

XmlNodeType Fragment May Contain
Element Any valid element content (for example, any combination of elements, comments, processing instructions, CDATA sections, text, and entity references).

An XML declaration can also be supplied. This allows you to specify the encoding for the XML fragment, rather than having to set it on the XmlParserContext object.
Attribute The value of an attribute (the part inside the quotes).
Document The contents of an entire XML document. This enforces document level rules.
See also

XmlTextReader(String, TextReader, XmlNameTable) XmlTextReader(String, TextReader, XmlNameTable) XmlTextReader(String, TextReader, XmlNameTable) XmlTextReader(String, TextReader, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified URL, TextReader and XmlNameTable.

public:
 XmlTextReader(System::String ^ url, System::IO::TextReader ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (string url, System.IO.TextReader input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : string * System.IO.TextReader * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (url As String, input As TextReader, nt As XmlNameTable)

Parameters

url
String String String String

The URL to use for resolving external resources. The BaseURI is set to this value. If url is null, BaseURI is set to String.Empty.

input
TextReader TextReader TextReader TextReader

The TextReader containing the XML data to read.

nt
XmlNameTable XmlNameTable XmlNameTable XmlNameTable

The XmlNameTable to use.

Exceptions

Remarks

Note

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.

It is assumed that the TextReader is already set to the correct encoding. This is used by clients that have already read some things from the stream in a multi-part MIME scenario.

See also

XmlTextReader(String, Stream, XmlNameTable) XmlTextReader(String, Stream, XmlNameTable) XmlTextReader(String, Stream, XmlNameTable) XmlTextReader(String, Stream, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified URL, stream and XmlNameTable.

public:
 XmlTextReader(System::String ^ url, System::IO::Stream ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (string url, System.IO.Stream input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : string * System.IO.Stream * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (url As String, input As Stream, nt As XmlNameTable)

Parameters

url
String String String String

The URL to use for resolving external resources. The BaseURI is set to this value. If url is null, BaseURI is set to String.Empty.

input
Stream Stream Stream Stream

The stream containing the XML data to read.

nt
XmlNameTable XmlNameTable XmlNameTable XmlNameTable

The XmlNameTable to use.

Exceptions

Remarks

Note

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.

See also

XmlTextReader(Stream, XmlNodeType, XmlParserContext) XmlTextReader(Stream, XmlNodeType, XmlParserContext) XmlTextReader(Stream, XmlNodeType, XmlParserContext) XmlTextReader(Stream, XmlNodeType, XmlParserContext)

Initializes a new instance of the XmlTextReader class with the specified stream, XmlNodeType, and XmlParserContext.

public:
 XmlTextReader(System::IO::Stream ^ xmlFragment, System::Xml::XmlNodeType fragType, System::Xml::XmlParserContext ^ context);
public XmlTextReader (System.IO.Stream xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext context);
new System.Xml.XmlTextReader : System.IO.Stream * System.Xml.XmlNodeType * System.Xml.XmlParserContext -> System.Xml.XmlTextReader
Public Sub New (xmlFragment As Stream, fragType As XmlNodeType, context As XmlParserContext)

Parameters

xmlFragment
Stream Stream Stream Stream

The stream containing the XML fragment to parse.

fragType
XmlNodeType XmlNodeType XmlNodeType XmlNodeType

The XmlNodeType of the XML fragment. This also determines what the fragment can contain. (See table below.)

context
XmlParserContext XmlParserContext XmlParserContext XmlParserContext

The XmlParserContext in which the xmlFragment is to be parsed. This includes the XmlNameTable to use, encoding, namespace scope, the current xml:lang, and the xml:space scope.

Exceptions

fragType is not an Element, Attribute, or Document XmlNodeType.

Remarks

Note

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.

This constructor parses the given string as a fragment of XML. If the XML fragment is an element or attribute, you can bypass the root level rules for well-formed XML documents.

The following table lists valid values for fragType.

XmlNodeType Fragment May Contain
Element Any valid element content (for example, any combination of elements, comments, processing instructions, CDATA sections, text, and entity references).

An XML declaration can also be supplied. This allows you to specify the encoding for the XML fragment, rather than having to set it on the XmlParserContext object.
Attribute The value of an attribute (the part inside the quotes).
Document The contents of an entire XML document. This enforces document level rules.

The reader uses the following to determine the encoding of the stream.

  1. Checks the XmlParserContext.Encoding property to determine the encoding.

  2. If the Encoding property is null, the reader checks for a byte-order mark at the beginning of the stream.

  3. If the Encoding property is null, and no byte-order mark is found, the reader assumes the stream is encoded in UTF-8.

See also

XmlTextReader(String, TextReader) XmlTextReader(String, TextReader) XmlTextReader(String, TextReader) XmlTextReader(String, TextReader)

Initializes a new instance of the XmlTextReader class with the specified URL and TextReader.

public:
 XmlTextReader(System::String ^ url, System::IO::TextReader ^ input);
public XmlTextReader (string url, System.IO.TextReader input);
new System.Xml.XmlTextReader : string * System.IO.TextReader -> System.Xml.XmlTextReader
Public Sub New (url As String, input As TextReader)

Parameters

url
String String String String

The URL to use for resolving external resources. The BaseURI is set to this value.

input
TextReader TextReader TextReader TextReader

The TextReader containing the XML data to read.

Remarks

Note

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.

It is assumed that the TextReader is already set to the correct encoding. This is used by clients that have already read some things from the stream in a multi-part MIME scenario.

See also

XmlTextReader(String, Stream) XmlTextReader(String, Stream) XmlTextReader(String, Stream) XmlTextReader(String, Stream)

Initializes a new instance of the XmlTextReader class with the specified URL and stream.

public:
 XmlTextReader(System::String ^ url, System::IO::Stream ^ input);
public XmlTextReader (string url, System.IO.Stream input);
new System.Xml.XmlTextReader : string * System.IO.Stream -> System.Xml.XmlTextReader
Public Sub New (url As String, input As Stream)

Parameters

url
String String String String

The URL to use for resolving external resources. The BaseURI is set to this value.

input
Stream Stream Stream Stream

The stream containing the XML data to read.

Exceptions

Remarks

Note

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.

See also

XmlTextReader(String, XmlNameTable) XmlTextReader(String, XmlNameTable) XmlTextReader(String, XmlNameTable) XmlTextReader(String, XmlNameTable)

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

public:
 XmlTextReader(System::String ^ url, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (string url, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : string * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (url As String, nt As XmlNameTable)

Parameters

url
String String String String

The URL for the file containing the XML data to read.

nt
XmlNameTable XmlNameTable XmlNameTable XmlNameTable

The XmlNameTable to use.

Exceptions

The remote filename cannot be resolved.

-or-

An error occurred while processing the request.

Remarks

Note

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.

See also

XmlTextReader(Stream, XmlNameTable) XmlTextReader(Stream, XmlNameTable) XmlTextReader(Stream, XmlNameTable) XmlTextReader(Stream, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified stream and XmlNameTable.

public:
 XmlTextReader(System::IO::Stream ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (System.IO.Stream input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : System.IO.Stream * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (input As Stream, nt As XmlNameTable)

Parameters

input
Stream Stream Stream Stream

The stream containing the XML data to read.

nt
XmlNameTable XmlNameTable XmlNameTable XmlNameTable

The XmlNameTable to use.

Exceptions

Remarks

Note

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.

The XmlTextReader decodes the stream using System.Text.Encoding.

If you specify a name table, this constructor uses the names defined already in that table.

See also

XmlTextReader(XmlNameTable) XmlTextReader(XmlNameTable) XmlTextReader(XmlNameTable) XmlTextReader(XmlNameTable)

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

protected:
 XmlTextReader(System::Xml::XmlNameTable ^ nt);
protected XmlTextReader (System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Protected Sub New (nt As XmlNameTable)

Parameters

nt
XmlNameTable XmlNameTable XmlNameTable XmlNameTable

The XmlNameTable to use.

Remarks

Note

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.

See also

XmlTextReader(String) XmlTextReader(String) XmlTextReader(String) XmlTextReader(String)

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

public:
 XmlTextReader(System::String ^ url);
public XmlTextReader (string url);
new System.Xml.XmlTextReader : string -> System.Xml.XmlTextReader
Public Sub New (url As String)

Parameters

url
String String String String

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

Exceptions

The remote filename cannot be resolved.

-or-

An error occurred while processing the request.

Examples

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   String^ filename = "items.xml";
   try
   {
      
      // Load the reader with the data file and ignore all white space nodes.         
      reader = gcnew 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 != nullptr )
            reader->Close();
   }

}

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
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

'Reads an XML document
Public Class Sample
    Private Const filename As String = "items.xml"
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        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()
                Select Case reader.NodeType
                    Case XmlNodeType.Element
                        Console.Write("<{0}>", reader.Name)
                    Case XmlNodeType.Text
                        Console.Write(reader.Value)
                    Case XmlNodeType.CDATA
                        Console.Write("<![CDATA[{0}]]>", reader.Value)
                    Case XmlNodeType.ProcessingInstruction
                        Console.Write("<?{0} {1}?>", reader.Name, reader.Value)
                    Case XmlNodeType.Comment
                        Console.Write("<!--{0}-->", reader.Value)
                    Case XmlNodeType.XmlDeclaration
                        Console.Write("<?xml version='1.0'?>")
                    Case XmlNodeType.Document
                    Case XmlNodeType.DocumentType
                        Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value)
                    Case XmlNodeType.EntityReference
                        Console.Write(reader.Name)
                    Case XmlNodeType.EndElement
                        Console.Write("</{0}>", reader.Name)
                End Select
            End While
        
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub 'Main 
End Class 'Sample

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>

Remarks

Note

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.

Note

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);  
See also

XmlTextReader(TextReader) XmlTextReader(TextReader) XmlTextReader(TextReader) XmlTextReader(TextReader)

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

public:
 XmlTextReader(System::IO::TextReader ^ input);
public XmlTextReader (System.IO.TextReader input);
new System.Xml.XmlTextReader : System.IO.TextReader -> System.Xml.XmlTextReader
Public Sub New (input As TextReader)

Parameters

input
TextReader TextReader TextReader TextReader

The TextReader containing the XML data to read.

Examples

The following example loads an XML string into the XmlTextReader object using the StringReader class.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   String^ xmlData = "<book>\r\n       <title>Oberon's Legacy</title>\r\n       <price>5.95</price>\r\n      </book>";
   
   // Create the reader.
   XmlTextReader^ reader = gcnew XmlTextReader( gcnew StringReader( xmlData ) );
   reader->WhitespaceHandling = WhitespaceHandling::None;
   
   // Display each element node.
   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::EndElement:
            Console::Write( "</{0}>", reader->Name );
            break;
      }
   }

   
   // Close the reader.
   reader->Close();
}

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

public class Sample {

  public static void Main() {

    string xmlData = 
    @"<book>
       <title>Oberon's Legacy</title>
       <price>5.95</price>
      </book>";

    // Create the reader.
    XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
    reader.WhitespaceHandling = WhitespaceHandling.None;

    // Display each element node.
    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.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
      }       
    }           

    // Close the reader.
    reader.Close();       
  }
} // End class
Imports System
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main()

    Dim xmlData as string 
    xmlData = "<book>" & _
              "  <title>Oberon's Legacy</title>" & _
              "  <price>5.95</price>" & _
              "</book>"

    ' Create the reader.
    Dim reader as XmlTextReader = new XmlTextReader(new StringReader(xmlData))
    reader.WhitespaceHandling = WhitespaceHandling.None

    ' Display each element node.
    while reader.Read()
       select case reader.NodeType
         case XmlNodeType.Element
           Console.Write("<{0}>", reader.Name)
         case XmlNodeType.Text
           Console.Write(reader.Value)
         case XmlNodeType.EndElement
           Console.Write("</{0}>", reader.Name)
       end select       
    end while           

    ' Close the reader.
    reader.Close()       
  end sub
end class

Remarks

Note

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.

It is assumed that the TextReader is already set to the correct encoding. This is used by clients that have already read some things from the stream in a multi-part MIME scenario.

See also

XmlTextReader(Stream) XmlTextReader(Stream) XmlTextReader(Stream) XmlTextReader(Stream)

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

public:
 XmlTextReader(System::IO::Stream ^ input);
public XmlTextReader (System.IO.Stream input);
new System.Xml.XmlTextReader : System.IO.Stream -> System.Xml.XmlTextReader
Public Sub New (input As Stream)

Parameters

input
Stream Stream Stream Stream

The stream containing the XML data to read.

Exceptions

Remarks

Note

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.

The XmlTextReader decodes the stream using System.Text.Encoding.

See also

XmlTextReader(TextReader, XmlNameTable) XmlTextReader(TextReader, XmlNameTable) XmlTextReader(TextReader, XmlNameTable) XmlTextReader(TextReader, XmlNameTable)

Initializes a new instance of the XmlTextReader class with the specified TextReader and XmlNameTable.

public:
 XmlTextReader(System::IO::TextReader ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (System.IO.TextReader input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : System.IO.TextReader * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (input As TextReader, nt As XmlNameTable)

Parameters

input
TextReader TextReader TextReader TextReader

The TextReader containing the XML data to read.

nt
XmlNameTable XmlNameTable XmlNameTable XmlNameTable

The XmlNameTable to use.

Exceptions

Remarks

Note

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.

It is assumed that the TextReader is already set to the correct encoding. This is used by clients that have already read some things from the stream in a multi-part MIME scenario.

See also

Applies to