Friday, November 13, 2009

Read XML doc with xmlns using XDocument

Sample Xml File County.xml

<root xmlns="nameSpace">
<county>
<group name="Sutter">
<group name="Yuba">
<group name="Placer">
</group>
</group>
<root>

-----------------------------------------------------------

//C#
XDocument xDoc = XDocument.Load("Country.xml");
XNamespace ns = xDoc.Root.Name.Namespace;

//adding the namespace infront of the XName will allow you to access the node.
var elements = xDoc..Element(ns+"root").Element(ns+"County").Descendants(ns+"Group");

foreach (var element in elements)
{
Console.Write(element.Attribute("name").ToString());
}




No comments:

Post a Comment