Extensions.XPathSelectElement selects an XElement using a XPath expression : XPath « XML « C# / C Sharp






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

1.XPathNavigator
2.XPathNodeIterator
3.Select by path
4.Find Elements with an XPath SearchFind Elements with an XPath Search
5.XPath Query Demo
6.Read XML node using the XML path
7.XmlQuery Example
8. XPath Expression Syntax
9.Using XPath to get string value, integer value and boolean value
10.Extensions.XPathSelectElements Selects a collection of elements using an XPath expression.
11.Returns the string result from evaluating an xpath expression against the given document and context.
12.Gets the text value from the element located by the given XPath.
13.Returns the inner text of the single node selected from the specified xpath if found; otherwise, null.
14.Get an array of nodes matching an XPath expression
15.Schema Reference Util