This documentation is archived and is not being maintained.

XmlTextReader Constructor

.NET Framework 1.1

Initializes a new instance of the XmlTextReader.

Overload List

Initializes a new instance of the XmlTextReader.

Supported by the .NET Compact Framework.

[Visual Basic] Protected Sub New()
[C#] protected XmlTextReader();
[C++] protected: XmlTextReader();
[JScript] protected function XmlTextReader();

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream)
[C#] public XmlTextReader(Stream);
[C++] public: XmlTextReader(Stream*);
[JScript] public function XmlTextReader(Stream);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public XmlTextReader(string);
[C++] public: XmlTextReader(String*);
[JScript] public function XmlTextReader(String);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(TextReader)
[C#] public XmlTextReader(TextReader);
[C++] public: XmlTextReader(TextReader*);
[JScript] public function XmlTextReader(TextReader);

This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Supported by the .NET Compact Framework.

[Visual Basic] Protected Sub New(XmlNameTable)
[C#] protected XmlTextReader(XmlNameTable);
[C++] protected: XmlTextReader(XmlNameTable*);
[JScript] protected function XmlTextReader(XmlNameTable);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, XmlNameTable)
[C#] public XmlTextReader(Stream, XmlNameTable);
[C++] public: XmlTextReader(Stream*, XmlNameTable*);
[JScript] public function XmlTextReader(Stream, XmlNameTable);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Stream)
[C#] public XmlTextReader(string, Stream);
[C++] public: XmlTextReader(String*, Stream*);
[JScript] public function XmlTextReader(String, Stream);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, TextReader)
[C#] public XmlTextReader(string, TextReader);
[C++] public: XmlTextReader(String*, TextReader*);
[JScript] public function XmlTextReader(String, TextReader);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, XmlNameTable)
[C#] public XmlTextReader(string, XmlNameTable);
[C++] public: XmlTextReader(String*, XmlNameTable*);
[JScript] public function XmlTextReader(String, XmlNameTable);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(TextReader, XmlNameTable)
[C#] public XmlTextReader(TextReader, XmlNameTable);
[C++] public: XmlTextReader(TextReader*, XmlNameTable*);
[JScript] public function XmlTextReader(TextReader, XmlNameTable);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, XmlNodeType, XmlParserContext)
[C#] public XmlTextReader(Stream, XmlNodeType, XmlParserContext);
[C++] public: XmlTextReader(Stream*, XmlNodeType, XmlParserContext*);
[JScript] public function XmlTextReader(Stream, XmlNodeType, XmlParserContext);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Stream, XmlNameTable)
[C#] public XmlTextReader(string, Stream, XmlNameTable);
[C++] public: XmlTextReader(String*, Stream*, XmlNameTable*);
[JScript] public function XmlTextReader(String, Stream, XmlNameTable);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, TextReader, XmlNameTable)
[C#] public XmlTextReader(string, TextReader, XmlNameTable);
[C++] public: XmlTextReader(String*, TextReader*, XmlNameTable*);
[JScript] public function XmlTextReader(String, TextReader, XmlNameTable);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, XmlNodeType, XmlParserContext)
[C#] public XmlTextReader(string, XmlNodeType, XmlParserContext);
[C++] public: XmlTextReader(String*, XmlNodeType, XmlParserContext*);
[JScript] public function XmlTextReader(String, XmlNodeType, XmlParserContext);

Example

[Visual Basic, C#, C++] The following example parses an XML fragment. It uses the XmlParserContext and its XmlNamespaceManager to handle namespace resolution.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the XmlTextReader constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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

[C#] 
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

[C++] 
#using <mscorlib.dll>
#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 =S"<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(S"bk", S"urn:sample");

   // Create the XmlParserContext.
   XmlParserContext* context = new XmlParserContext(0, nsmgr, 0, 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(S"< {0}>", reader -> LocalName);
         else 
         {
            Console::Write(S"< {0}: {1}>", reader -> Prefix, reader -> LocalName);
            Console::WriteLine(S" The namespace URI is {0}", reader -> NamespaceURI);
         }
      }
   }

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlTextReader Class | XmlTextReader Members | System.Xml Namespace

Show: