This documentation is archived and is not being maintained.

XmlTextReader Constructor (String, XmlNodeType, XmlParserContext)

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

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

public XmlTextReader(
	string xmlFragment,
	XmlNodeType fragType,
	XmlParserContext context


Type: System.String

The string containing the XML fragment to parse.

Type: System.Xml.XmlNodeType

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

Type: System.Xml.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.


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


xmlFragment is null.


In the .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the XmlReader.Create method. This allows you to take full advantage of the new features introduced in this release. For more information, see Creating XML Readers.

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.


Fragment May Contain


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.


The value of an attribute (the part inside the quotes).


The contents of an entire XML document. This enforces document level rules.

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

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>" +

    //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);
            Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName);
            Console.WriteLine(" The namespace URI is " + reader.NamespaceURI);

    //Close the reader.

} // End class
#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);
            Console::Write(S"< {0}: {1}>", reader -> Prefix, reader -> LocalName);
            Console::WriteLine(S" The namespace URI is {0}", reader -> NamespaceURI);

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0