Monday, November 05, 2007

Working with XML Namespaces : NET 2.0

If your Xml document has a default namespace, you can assign prefix, using
XmlNamespaceManager object.
Look it:

Xml Snippet

<?xml version="1.0" encoding="UTF-8"?>
<data xmlns="http://tempuri.org/" xmlns:dc=http://tempuri.org/1.1/>
<items>
<dc:item>Hello</dc:item>
<dc:item>World</dc:item>
</items>
...

Code Snippet

XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:\Bentz\data.xml");
...
XmlNamespaceManager nm = new XmlNamespaceManager(xdoc.NameTable);
nm.AddNamespace("ns", "http://tempuri.org/"); /* Default namespace*/
nm.AddNamespace("dc", "http://tempuri.org/1.1/");
...
//browsing a node
XmlNode xn = xdoc.SelectSingleNode("ns:data/ns:items/dc:item/text()", nm);
...

I read this article before:
http://support.microsoft.com/kb/318545


Good coding.

No comments:

Google