Figure 1 Returning All Namespaces // XPath namespace query sample by Bulent Ozkir (bulento@microsoft.com)
using System;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
public class XMLApp
{
public static void Main( String[] args)
{
try
{
// namespace_URI array
ArrayList arr1 = new ArrayList();
// namespace_name array
ArrayList arr2 = new ArrayList();
XmlDocument _doc = new XmlDocument();
_doc.LoadXml(@"<Schema name=""BlankSpecification""
xmlns=""urn:schemas-microsoft-com:xml-data""
xmlns:b=""urn:schemas-microsoft-com:BizTalkServer""
xmlns:d=""urn:schemas-microsoft-com:datatypes""></Schema>");
XmlNodeList nl2 = _doc.SelectNodes(@"//namespace::node()");
foreach (XmlNode n in nl2)
{
if( arr1.IndexOf(n.Value) == -1 && arr2.IndexOf(n.Name) == -1)
{
Console.WriteLine("<" + n.Value + ">");
Console.WriteLine("<" + n.Name + ">");
arr1.Add(n.Value);
arr2.Add(n.Name);
}
}
Console.WriteLine("Total Number of namespace URIs=" + arr1.Count);
Console.WriteLine("Total Number of namespace names=" + arr2.Count);
/* output:
<urn:schemas-microsoft-com:datatypes>
<xmlns:d>
<urn:schemas-microsoft-com:BizTalkServer>
<xmlns:b>
<urn:schemas-microsoft-com:xml-data>
<xmlns>
<http://www.w3.org/XML/1998/namespace>
<xmlns:xml>
Total Number of namespace URIs=4
Total Number of namespace names=4
*/
}
catch(Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
}
}
|