Q:

how i can list out all the namespace in XML? 

My basic requirement is to get element value from the XML file, i have used XMLDoxument. SelectSingleNode.  My XML file contains some Namespace in header, so i have used NameSpaceManager to add namespace-prefix and i have used prefix to get that particular element.  Now in my XML files that namespaces are getting vary, i  don’t want to do any hard coding, is there any way that i can find out all the namespaces and i can add it to NameSpaceManager.  
Thanks. 

A:

Namespaces can be found anywhere inside xml document.  So to extract all namespaces and declare them into a XmlNamespaceManager I did the following:
public static XmlNamespaceManager getAllNamespaces(XmlDocument xDoc)
{
    XmlNamespaceManager result = new XmlNamespaceManager(xDoc. NameTable);

    IDictionary<string, string> localNamespaces = null;
    XPathNavigator xNav = xDoc. CreateNavigator();
    while (xNav. MoveToFollowing(XPathNodeType. Element))
    {
        localNamespaces = xNav. GetNamespacesInScope(XmlNamespaceScope. Local);
        foreach (var localNamespace in localNamespaces)
        {
            string prefix = localNamespace. Key;
            if (string. IsNullOrEmpty(prefix))
                    prefix = "DEFAULT";

            result. AddNamespace(prefix, localNamespace. Value);
        }
    }

    return result;
}

Just pay attention to the default namespace case.  I redefined default namespace as "DEFAULT" prefix because I had problems when making SelectSingleNode using the above returned namespace manager when querying the default namespace.  I was my workaround.  
Hope this helps

A:

Thanks for your quick response...
I think the .Net version that you are using is must be latest one.  I am using .Net framework 1.1 ... pretty old :( .. 
By the time,, i have got some sample code like this.. for the same purpose...
XmlNodeList _xmlNameSpaceList =  _xmlDocument. SelectNodes(@"//namespace::*[not(.  = ../../namespace::*)]");

            _xmlNSmgr = new XmlNamespaceManager(_xmlDocument. NameTable);        

            foreach(XmlNode nsNode in _xmlNameSpaceList)
            {
                _xmlNSmgr. AddNamespace(nsNode. LocalName,nsNode. Value);
            }

Any comment will be appreciated to add knowledge to my KB...
Thanks

