Select All Child Nodes : XPath « XML « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

public class MainClass
{

    public static void Main()
    {
        XmlDocument mDocument = new XmlDocument();
        XmlNode mCurrentNode;

        mDocument.Load("XPathQuery.xml");
        mCurrentNode = mDocument.DocumentElement;


        XmlNodeList nodeList = mCurrentNode.SelectNodes("*");
        DisplayList(nodeList);
    }

    static void DisplayList(XmlNodeList nodeList)
    {
        foreach (XmlNode node in nodeList)
        {
            RecurseXmlDocumentNoSiblings(node);
        }
    }

    static void RecurseXmlDocumentNoSiblings(XmlNode root)
    {
        if (root is XmlElement)
        {
            Console.WriteLine(root.Name);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
        }
        else if (root is XmlText)
        {
            string text = ((XmlText)root).Value;
            Console.WriteLine(text);
        }
        else if (root is XmlComment)
        {
            string text = root.Value;
            Console.WriteLine(text);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
        }
    }
    static void RecurseXmlDocument(XmlNode root)
    {
        if (root is XmlElement)
        {
            Console.WriteLine(root.Name);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
            if (root.NextSibling != null)
                RecurseXmlDocument(root.NextSibling);
        }
        else if (root is XmlText)
        {
            string text = ((XmlText)root).Value;
            Console.WriteLine(text);
        }
        else if (root is XmlComment)
        {
            string text = root.Value;
            Console.WriteLine(text);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
            if (root.NextSibling != null)
                RecurseXmlDocument(root.NextSibling);
        }
    }
}








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