XPath selecting: descendant::Customer[@FirstName='D'] : XPath « XML « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public override string ToString()
    {
        return string.Format("{0} {1}\nEmail:   {2}", FirstName, LastName, EmailAddress);
    }
}
public class Tester
{
    static void Main()
    {
        List<Customer> customers = new List<Customer>{
                new Customer { FirstName = "A", 
                               LastName = "G",
                               EmailAddress = "o@a.com"},
                new Customer { FirstName = "B", 
                               LastName = "C",
                               EmailAddress = "k@a.com" },
                new Customer { FirstName = "D", 
                               LastName = "C",
                               EmailAddress = "d@a.com" },
                new Customer { FirstName = "E", 
                               LastName = "G",
                               EmailAddress = "j@a.com" },
                new Customer { FirstName = "L", 
                               LastName = "H",
                               EmailAddress = "l@a.com" }
            };

        XmlDocument customerXml = new XmlDocument();
        XmlElement rootElem = customerXml.CreateElement("Customers");
        customerXml.AppendChild(rootElem);
        foreach (Customer customer in customers)
        {
            XmlElement customerElem = customerXml.CreateElement("Customer");
            XmlAttribute firstNameAttr = customerXml.CreateAttribute("FirstName");
            firstNameAttr.Value = customer.FirstName;
            customerElem.Attributes.Append(firstNameAttr);

            XmlAttribute lastNameAttr = customerXml.CreateAttribute("LastName");
            lastNameAttr.Value = customer.LastName;
            customerElem.Attributes.Append(lastNameAttr);

            XmlElement emailAddress = customerXml.CreateElement("EmailAddress");
            emailAddress.InnerText = customer.EmailAddress;
            customerElem.AppendChild(emailAddress);

            rootElem.AppendChild(customerElem);
        }


        string xPath = "descendant::Customer[@FirstName='D']";
        XmlNode oneCustomer = customerXml.SelectSingleNode(xPath);
        Console.WriteLine("\nSelectSingleNode(\"{0}\")...", xPath);
        if (oneCustomer != null)
            Console.WriteLine(oneCustomer.OuterXml);
        else
            Console.WriteLine("Not found");
    }
}








30.21.XPath
30.21.1.XPath Select Nodes
30.21.2.Append child from XPathNavigator
30.21.3.Append new node to XPathNavigator
30.21.4.Using XPath to filter data from Database
30.21.5.Using XPathNavigator to loop through code
30.21.6.Using XPathNodeIterator
30.21.7.XPath Navigator
30.21.8.XPath Node Iterator
30.21.9.XPath Selecting: /Customers/Customer[@FirstName='D']
30.21.10.XPath XSLT transformation
30.21.11.XPath selecting: descendant::Customer[@FirstName='D']
30.21.12.XPath selecting: descendant::Customer[EmailAddress='k@a.com']
30.21.13.XPath selecting: descendant::Customer[attribute::FirstName='D']
30.21.14.XPath selecting: descendant::Customer[contains(EmailAddress, 'a.com')]
30.21.15.XPath selecting: descendant::Customer[starts-with(@LastName, 'G') and contains(EmailAddress, 'a.com')]
30.21.16.XPath selecting: descendant::EmailAddress[text()='k@a.com']
30.21.17.XPathNavigator and TreeView
30.21.18.Xpath Demo: //attribute[@name='capacity'] | //attribute[@name='weight']
30.21.19.Xpath Demo: /items/item
30.21.20.Xpath Demo: /items/item/attribute[@value > 10 and @name='weight']
30.21.21.Xpath Demo: /items/item[2]
30.21.22.Create XPathNavigator
30.21.23.Move to first child in XPathNavigator
30.21.24.Compile XPath to create XPathExpression
30.21.25.Read sub tree from XPathNavigator
30.21.26.Select All Author Nodes
30.21.27.Select All Books
30.21.28.Select All Child Nodes
30.21.29.Select XPath from XPathNavigator
30.21.30.Set new value through XPathNavigator
30.21.31.Show innter xml and outer xml from XPathNavigator
30.21.32.Sorting XPathExpression with XmlSortOrder.Ascending, XmlCaseOrder.None
30.21.33.Using XPathExpression to sort by first name
30.21.34.Select node with asterisk
30.21.35.Use the XPath return type to determine how to process the XPath expression