Description





Description

This class stores prefixes and namespaces as unique String objects.

XmlNamespaceManager assumes all prefixes and namespaces are valid.

If the prefix and namespace already exist within the current scope, they 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)

System.String.Empty

System.String.Empty. The empty namespace can be reassigned a different prefix. For example, xmlns="" defines the default namespace to be the empty namespace.


Example

using System;

using System.Xml;



/// <summary>

/// Creates a new XmlNamespaceManager based on an XmlTextReader

/// Demonstrates adding and removing namespaces, and displaying

/// namespace information

/// </summary>



public class XmlNamespaceManagerSample

{

    public static void Main()

    {

        XmlTextReader r = new XmlTextReader("sample.xml");

        XmlNamespaceManager nm = new XmlNamespaceManager(r.NameTable);

        nm.AddNamespace("", "http://www.default");

        string ns = nm.DefaultNamespace;

        Console.WriteLine("DefaultNamespace Prefix: '{0}', "

            + "Namespace: '{1}'",

            nm.LookupPrefix(ns), ns);

        nm.AddNamespace("test", "http://www.test");

        ShowNamespaces("Before:", nm);

        if (nm.HasNamespace("test"))

        {

            string tns = nm.LookupNamespace("test");

            nm.RemoveNamespace("test", tns);

        }

        ShowNamespaces("After RemoveNamespace:", nm);

        r.Close();

        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }



    private static void ShowNamespaces(string s,

        XmlNamespaceManager nm)

    {

        Console.WriteLine();

        Console.WriteLine(s);

        foreach (string p in nm)

        {

            Console.WriteLine("Prefix: '{0}', Namespace: '{1}'",

                p, nm.LookupNamespace(p));

        }

    }

}


The output is


DefaultNamespace Prefix: '', Namespace: 'http://www.default'



Before:

Prefix: 'test', Namespace: 'http://www.test'

Prefix: 'xmlns', Namespace: 'http://www.w3.org/2000/xmlns/'

Prefix: 'xml', Namespace: 'http://www.w3.org/XML/1998/namespace'

Prefix: '', Namespace: 'http://www.default'



After RemoveNamespace:

Prefix: 'xml', Namespace: 'http://www.w3.org/XML/1998/namespace'

Prefix: 'xmlns', Namespace: 'http://www.w3.org/2000/xmlns/'

Prefix: '', Namespace: 'http://www.default'





Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows