Extensions.XPathSelectElement selects an XElement using a XPath expression
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml.XPath;
using System.IO;
public class MainClass
{
public static void Main()
{
string markup = @"
<aw:Root xmlns:aw='http://www.domain.com'>
<aw:Child1>child one data</aw:Child1>
<aw:Child2>child two data</aw:Child2>
</aw:Root>";
XmlReader reader = XmlReader.Create(new StringReader(markup));
XElement root = XElement.Load(reader);
XmlNameTable nameTable = reader.NameTable;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("aw", "http://www.domain.com");
XElement child1 = root.XPathSelectElement("./aw:Child1", namespaceManager);
Console.WriteLine(child1);
}
}
Related examples in the same category