System.Xml.XmlParserContext Class

Assembly: System.Xml.dll
Namespace: System.Xml
Summary
Provides all the context information required by XmlTextReader or XmlValidatingReader to parse an XML fragment.
C# Syntax:
public class XmlParserContext
See also:
System.Xml Namespace

System.Xml.XmlParserContext Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(XmlNameTable nt, XmlNamespaceManager nsMgr, string xmlLang, XmlSpace xmlSpace)

Initializes a new instance of the XmlParserContext class with the specified values.
ctor #2 Overloaded:
.ctor(XmlNameTable nt, XmlNamespaceManager nsMgr, string xmlLang, XmlSpace xmlSpace, Encoding enc)

Initializes a new instance of the XmlParserContext class with the specified values.
ctor #3 Overloaded:
.ctor(XmlNameTable nt, XmlNamespaceManager nsMgr, string docTypeName, string pubId, string sysId, string internalSubset, string baseURI, string xmlLang, XmlSpace xmlSpace)

Initializes a new instance of the XmlParserContext class with the specified values.
ctor #4 Overloaded:
.ctor(XmlNameTable nt, XmlNamespaceManager nsMgr, string docTypeName, string pubId, string sysId, string internalSubset, string baseURI, string xmlLang, XmlSpace xmlSpace, Encoding enc)

Initializes a new instance of the XmlParserContext class with the specified values.
Public Properties
BaseURI Read-write

Gets or sets the base URI.
DocTypeName Read-write

Gets or sets the name of the document type declaration.
Encoding Read-write

InternalSubset Read-write

Gets or sets the internal DTD subset.
NamespaceManager Read-write

Gets or sets the XmlNamespaceManager.
NameTable Read-write

Gets the XmlNameTable used to atomize strings. For more information on atomized strings, see XmlNameTable.
PublicId Read-write

Gets or sets the public identifier.
SystemId Read-write

Gets or sets the system identifier.
XmlLang Read-write

Gets or sets the current xml:lang scope.
XmlSpace Read-write

Gets or sets the current xml:space scope.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.XmlParserContext Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the XmlParserContext class with the specified values.
C# Syntax:
public XmlParserContext(
   XmlNameTable nt,
   XmlNamespaceManager nsMgr,
   string xmlLang,
   XmlSpace xmlSpace
);
Parameters:

nt

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 on atomized strings, see XmlNameTable.

nsMgr

The XmlNamespaceManager to use for looking up namespace information.

xmlLang

The xml:lang scope.

xmlSpace

An XmlSpace value indicating the xml:space scope.

Remarks
If nt and the name table used to construct the nsMgr are not the same, XmlParserContext does not recognize namespaces added to nsMgr and throws an XmlException when it encounters the unrecognized namespaces.
Example
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()
  {
    XmlValidatingReader reader = null;

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

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

     } 

     finally 
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class

    

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the XmlParserContext class with the specified values.
C# Syntax:
public XmlParserContext(
   XmlNameTable nt,
   XmlNamespaceManager nsMgr,
   string xmlLang,
   XmlSpace xmlSpace,
   Encoding enc
);
Parameters:

nt

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 on atomized strings, see XmlNameTable.

nsMgr

The XmlNamespaceManager to use for looking up namespace information.

xmlLang

The xml:lang scope.

xmlSpace

An XmlSpace value indicating the xml:space scope.

enc

An Encoding object indicating the encoding setting.

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the XmlParserContext class with the specified values.
C# Syntax:
public XmlParserContext(
   XmlNameTable nt,
   XmlNamespaceManager nsMgr,
   string docTypeName,
   string pubId,
   string sysId,
   string internalSubset,
   string baseURI,
   string xmlLang,
   XmlSpace xmlSpace
);
Parameters:

nt

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 on atomized strings, see XmlNameTable.

nsMgr

The XmlNamespaceManager to use for looking up namespace information.

docTypeName

The name of the document type declaration.

pubId

The public identifier.

sysId

The system identifier.

internalSubset

The internal DTD subset.

baseURI

The base URI for the XML fragment (the location from which the fragment was loaded).

xmlLang

The xml:lang scope.

xmlSpace

An XmlSpace value indicating the xml:space scope.

Remarks
This constructor supplies all DocumentType information requpired by XmlValidatingReader. If this XmlParserContext is passed to an XmlTextReader, all DTD information is ignored.

If nt and the name table used to construct the nsMgr are not the same, XmlParserContext does not recognize namespaces added to nsMgr and throws an XmlException when it encounters the unrecognized namespaces.

Example
The following example uses an XmlParserContext to read an XML fragment.
using System;
using System.IO;
using System.Xml;

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

    try
    {
       //Create the XML fragment to be parsed.
       string xmlFrag ="<book genre='novel' misc='sale-item &h;'></book>";
 
       //Create the XmlParserContext. The XmlParserContext provides the 
       //necessary DTD information so that the entity reference can be expanded.
       XmlParserContext context;
       string subset = "<!ENTITY h 'hardcover'>";
       context = new XmlParserContext(null, null, "book", null, null, subset, "", "", XmlSpace.None);
        
       //Create the reader. 
       reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
       reader.ValidationType = ValidationType.None;
  
       //Read the all the attributes on the book element.
       reader.MoveToContent();
       while (reader.MoveToNextAttribute()){
          Console.WriteLine("{0} = {1}", reader.Name, reader.Value);
        } 
     } 
     finally 
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class

    

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the XmlParserContext class with the specified values.
C# Syntax:
public XmlParserContext(
   XmlNameTable nt,
   XmlNamespaceManager nsMgr,
   string docTypeName,
   string pubId,
   string sysId,
   string internalSubset,
   string baseURI,
   string xmlLang,
   XmlSpace xmlSpace,
   Encoding enc
);
Parameters:

nt

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 on atomized strings, see XmlNameTable.

nsMgr

The XmlNamespaceManager to use for looking up namespace information.

docTypeName

The name of the document type declaration.

pubId

The public identifier.

sysId

The system identifier.

internalSubset

The internal DTD subset.

baseURI

The base URI for the XML fragment (the location from which the fragment was loaded).

xmlLang

The xml:lang scope.

xmlSpace

An XmlSpace value indicating the xml:space scope.

enc

An Encoding object indicating the encoding setting.

Return to top


Property: BaseURI (read-write)
Summary
Gets or sets the base URI.
C# Syntax:
public string BaseURI {get; set;}
Remarks
A networked XML document is comprised of chunks of data aggregated using various W3C standard inclusion mechanisms and therefore may contain nodes that come from different places.BaseURI shows where these nodes originated.

Return to top


Property: DocTypeName (read-write)
Summary
Gets or sets the name of the document type declaration.
C# Syntax:
public string DocTypeName {get; set;}
Remarks
This property, along with XmlParserContext.PublicId, XmlParserContext.SystemId, and XmlParserContext.InternalSubset, provides all the document type declaration information. It is used to find and load the necessary DTD information so that you get all the default attributes and entities defined in the DTD.

Return to top


Property: Encoding (read-write)
Summary
Gets or sets the encoding type.
C# Syntax:
public Encoding Encoding {get; set;}

Return to top


Property: InternalSubset (read-write)
Summary
Gets or sets the internal DTD subset.
C# Syntax:
public string InternalSubset {get; set;}
Remarks
This property, along with XmlParserContext.DocTypeName, XmlParserContext.PublicId, and XmlParserContext.SystemId, provides all the document type declaration information. It is used to find and load the necessary DTD information so that you get all the default attributes and entities defined in the DTD.

Return to top


Property: NamespaceManager (read-write)
Summary
Gets or sets the XmlNamespaceManager.
C# Syntax:
public XmlNamespaceManager NamespaceManager {get; set;}
Remarks
The NamespaceManager defines the current namespace scope and provides methods for looking up namespace information.

Return to top


Property: NameTable (read-write)
Summary
Gets the XmlNameTable used to atomize strings. For more information on atomized strings, see XmlNameTable.
C# Syntax:
public XmlNameTable NameTable {get; set;}

Return to top


Property: PublicId (read-write)
Summary
Gets or sets the public identifier.
C# Syntax:
public string PublicId {get; set;}
Remarks
This property, along with XmlParserContext.DocTypeName, XmlParserContext.SystemId, and XmlParserContext.InternalSubset, provides all the document type declaration information. It is used to find and load the required DTD information so that you get all the default attributes and entities defined in the DTD.

Return to top


Property: SystemId (read-write)
Summary
Gets or sets the system identifier.
C# Syntax:
public string SystemId {get; set;}
Remarks
This property, along with XmlParserContext.DocTypeName, XmlParserContext.PublicId, and XmlParserContext.InternalSubset, provides all the document type declaration information. It is used to find and load the necessary DTD information so that you get all the default attributes and entities defined in the DTD.

Return to top


Property: XmlLang (read-write)
Summary
Gets or sets the current xml:lang scope.
C# Syntax:
public string XmlLang {get; set;}
Remarks
For details on valid xml:lang values, refer to section 2.12 of the W3C Extensible Markup Language (XML) 1.0 recommendation.

Return to top


Property: XmlSpace (read-write)
Summary
Gets or sets the current xml:space scope.
C# Syntax:
public XmlSpace XmlSpace {get; set;}

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlParserContext();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.