XPath selecting: descendant::Customer[contains(EmailAddress, 'a.com')] : XPath « XML « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » XML » XPath 
30.21.14.XPath selecting: descendant::Customer[contains(EmailAddress, 'a.com')]
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[contains(EmailAddress, 'a.com')]";
            XmlNodeList customerList = customerXml.SelectNodes(xPath);
            Console.WriteLine("\nSelectNodes(\"{0}\")...", xPath);
            if (customerList != null)
            {
                foreach (XmlNode customer in customerList)
                    Console.WriteLine(customer.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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.