XmlParserContext Constructor (XmlNameTable, XmlNamespaceManager, String, XmlSpace)


Initializes a new instance of the XmlParserContext class with the specified XmlNameTable, XmlNamespaceManager, xml:lang, and xml:space values.

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

public XmlParserContext(
	XmlNameTable nt,
	XmlNamespaceManager nsMgr,
	string xmlLang,
	XmlSpace xmlSpace


Type: System.Xml.XmlNameTable

The XmlNameTable to use to atomize strings. If this is null, the name table used to construct the nsMgr is used instead. For more information about atomized strings, see XmlNameTable.

Type: System.Xml.XmlNamespaceManager

The XmlNamespaceManager to use for looking up namespace information, or null.

Type: System.String

The xml:lang scope.

Type: System.Xml.XmlSpace

An XmlSpace value indicating the xml:space scope.

Exception Condition

nt is not the same XmlNameTable used to construct nsMgr.

The following example reads an XML fragment. It uses an XmlParserContext and its XmlNamespaceManager to handle namespace matching.

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

public class Sample
    public static void Main()
        XmlTextReader reader = null;

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

            //Create the XmlNamespaceManager that is used to
            //look up namespace information.
            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);

            //Implement the reader. 
            reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

            //Parse the XML fragment.  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);


            if (reader != null)
} // End class

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top