public class XmlNamespaceManager : IEnumerable
|
| ctor #1 | Initializes a new instance of the XmlNamespaceManager class with the specified XmlNameTable. |
| DefaultNamespace | Read-only Gets the namespace URI for the default namespace. |
| NameTable | Read-only Gets the XmlNameTable associated with this object. |
| AddNamespace | Adds the given namespace to the collection. |
| Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
| GetEnumerator | Provides support for the "foreach" style iteration over the collection of namespaces in the XmlNamespaceManager. |
| 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. |
| HasNamespace | Gets a value indicating whether the supplied prefix has a namespace defined for the current pushed scope. |
| LookupNamespace | Gets the namespace URI for the specified prefix. |
| LookupPrefix | Finds the prefix declared for the given namespace URI. |
| PopScope | Pops a namespace scope off the stack. |
| PushScope | Pushes a namespace scope onto the stack. |
| RemoveNamespace | Removes the given namespace for the given prefix. |
| ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
| 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:
public XmlNamespaceManager( |
nameTable
| Exception Type | Condition |
|---|---|
| NullReferenceException | null is passed to the constructor |
For more information on atomized strings, see XmlNameTable.
XmlTextReader reader = new XmlTextReader("myfile.xml");
XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books");
nsmanager.PushScope();
nsmanager.AddNamespace("msstore", "www.microsoft.com/store");
while (reader.Read())
{
Console.WriteLine("Reader Prefix:{0}", reader.Prefix);
Console.WriteLine("XmlNamespaceManager Prefix:{0}",
nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI)));
}
public virtual string DefaultNamespace {get;}
|
if (nsmgr.HasNamespace(String.Empty))
Console.WriteLine(nsmgr.DefaultNamespace);
public XmlNameTable NameTable {get;}
|
prefix
uri
| Exception Type | Condition |
|---|---|
| InvalidOperationException | The value for prefix is "xmlns". |
XmlReader checks names, including prefixes and namespaces, to ensure they are valid XML names according to the W3C specification.XmlNamespaceManager is used internally by XmlReader, so to avoid a duplication of efforts, XmlNamespaceManager assumes all prefixes and namespaces are valid.
If the prefix and namespace already exists within the current scope, it will replace the existing prefix/namespace combination. The same prefix and namespace combination can exist across different scopes.
The following prefix/namespace pairs are added by default to the XmlNamespaceManager. They can be determined at any scope.
| Prefix | Namespace |
|---|---|
| xmlns | http://www.w3.org/2000/xmlns/ (the xmlns prefix namespace) |
| xml | http://www.w3.org/XML/1998/namespace (The XML namespace) |
| String.Empty | String.Empty (The empty namespace). This value can be reassigned a different prefix. For example, xmlns="" defines the default namespace to be the empty namespace |
using System;
using System.IO;
using System.Xml;
//Reads an XML fragment
public class Sample
{
public static void Main()
{
try
{
//The string containing the XML to read
String xmlFrag="<book>" +
"<title>Pride And Prejudice</title>" +
"<author>" +
"<first-name>Jane</first-name>" +
"<last-name>Austen</last-name>" +
"</author>" +
"<curr:price>19.95</curr:price>" +
"<misc>&h;</misc>" +
"</book>";
ReadSample sample = new ReadSample(xmlFrag);
sample.ReadIt();
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
}
} // End class
//Reads an XML fragment
public class ReadSample
{
XmlValidatingReader reader = null;
public ReadSample(String xmlFrag)
{
//create an XmlNamespaceManager to resolve namespaces
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(String.Empty, "urn:samples"); //default namespace
nsmgr.AddNamespace("curr", "urn:samples:dollar");
//Create an XmlParserContext. The XmlParserContext contains all the information
//required to parse the XML fragment, including the entity information
XmlParserContext context;
String subset="<!ENTITY h 'hardcover'>";
context=new XmlParserContext(nt, nsmgr, "book", null, null, subset, null, null, XmlSpace.None);
//create the reader
reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
}
//Read the XML fragment
public void ReadIt()
{
try
{
while(reader.Read())
{
if (reader.HasValue)
Console.WriteLine("{0} [{1}] = {2}",reader.NodeType, reader.Name, reader.Value);
else
Console.WriteLine("{0} [{1}]",reader.NodeType, reader.Name);
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
}
} //End class
~XmlNamespaceManager(); |
public virtual IEnumerator GetEnumerator(); |
public virtual int GetHashCode(); |
public Type GetType(); |
prefix
if (nsmgr.HasNamespace(String.Empty))
Console.WriteLine(nsmgr.DefaultNamespace);
prefix
For more information on atomized strings, see XmlNameTable.
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
Sample test = new Sample();
}
public Sample()
{
//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
//Add prefix/namespace pairs to the XmlNamespaceManager.
nsmgr.AddNamespace("", "www.wideworldimporters.com"); //Adds a default namespace.
nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
nsmgr.PushScope(); //Pushes a namespace scope on the stack.
nsmgr.AddNamespace("", "www.lucernepublishing.com"); //Adds another default namespace.
nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners");
Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...");
ShowAllNamespaces(nsmgr);
}
private void ShowAllNamespaces(XmlNamespaceManager nsmgr)
{
do{
foreach (String prefix in nsmgr)
{
Console.WriteLine("Prefix={0}, Namespace={1}", prefix,nsmgr.LookupNamespace(prefix));
}
}
while (nsmgr.PopScope());
}
}
uri
The returned string is also atomized. For more information on atomized strings, see XmlNameTable.
String prefix = nsmgr.LookupPrefix("www.wideworldimporters.com/europe");
nsmgr.RemoveNamespace(prefix, "www.wideworldimporters.com/europe");
protected object MemberwiseClone(); |
public virtual bool PopScope(); |
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
Sample test = new Sample();
}
public Sample()
{
//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
//Add prefix/namespace pairs to the XmlNamespaceManager.
nsmgr.AddNamespace("", "www.wideworldimporters.com"); //Adds a default namespace.
nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
nsmgr.PushScope(); //Pushes a namespace scope on the stack.
nsmgr.AddNamespace("", "www.lucernepublishing.com"); //Adds another default namespace.
nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners");
Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...");
ShowAllNamespaces(nsmgr);
}
private void ShowAllNamespaces(XmlNamespaceManager nsmgr)
{
do{
foreach (String prefix in nsmgr)
{
Console.WriteLine("Prefix={0}, Namespace={1}", prefix,nsmgr.LookupNamespace(prefix));
}
}
while (nsmgr.PopScope());
}
}
public virtual void PushScope(); |
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
Sample test = new Sample();
}
public Sample()
{
//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
//Add prefix/namespace pairs to the XmlNamespaceManager.
nsmgr.AddNamespace("", "www.wideworldimporters.com"); //Adds a default namespace.
nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
nsmgr.PushScope(); //Pushes a namespace scope on the stack.
nsmgr.AddNamespace("", "www.lucernepublishing.com"); //Adds another default namespace.
nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners");
Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...");
ShowAllNamespaces(nsmgr);
}
private void ShowAllNamespaces(XmlNamespaceManager nsmgr)
{
do{
foreach (String prefix in nsmgr)
{
Console.WriteLine("Prefix={0}, Namespace={1}", prefix,nsmgr.LookupNamespace(prefix));
}
}
while (nsmgr.PopScope());
}
}
prefix
uri
public virtual string ToString(); |