XmlQuery Example : XPath « XML « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » XML » XPathScreenshots 
XmlQuery Example


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

class Form1 : Form {
    private XmlDocument mDocument;
    private XmlNode mCurrentNode;

    public Form1() {
        InitializeComponent();

        mDocument = new XmlDocument();
        mDocument.Load("XPathQuery.xml");
        mCurrentNode = mDocument.DocumentElement;
        ClearListBox();
    }

    private void DisplayList(XmlNodeList nodeList) {
        foreach (XmlNode node in nodeList) {
            RecurseXmlDocumentNoSiblings(node, 0);
        }
    }

    private void RecurseXmlDocumentNoSiblings(XmlNode root, int indent) {
        if (root == null)
            return;

        if (root is XmlElement) {
            listBoxResult.Items.Add(root.Name.PadLeft(root.Name.Length + indent));

            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild, indent + 2);
        else if (root is XmlText) {
            string text = ((XmlText)root).Value;
            listBoxResult.Items.Add(text.PadLeft(text.Length + indent));
        else if (root is XmlComment) {
            string text = root.Value;
            listBoxResult.Items.Add(text.PadLeft(text.Length + indent));

            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild, indent + 2);
        }
    }

    private void RecurseXmlDocument(XmlNode root, int indent) {
        if (root == null)
            return;
        if (root is XmlElement) {
            listBoxResult.Items.Add(root.Name.PadLeft(root.Name.Length + indent));
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild, indent + 2);
            if (root.NextSibling != null)
                RecurseXmlDocument(root.NextSibling, indent);
        else if (root is XmlText) {
            string text = ((XmlText)root).Value;
            listBoxResult.Items.Add(text.PadLeft(text.Length + indent));
        else if (root is XmlComment) {
            string text = root.Value;
            listBoxResult.Items.Add(text.PadLeft(text.Length + indent));
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild, indent + 2);
            if (root.NextSibling != null)
                RecurseXmlDocument(root.NextSibling, indent);
        }
    }

    private void ClearListBox() {
        listBoxResult.Items.Clear();
    }
    private void radioButtonSelectRoot_CheckedChanged(object sender, EventArgs e) {
        mCurrentNode = mCurrentNode.SelectSingleNode("//books");
        ClearListBox();
        RecurseXmlDocument(mCurrentNode, 0);
    }

    private void buttonClose_Click(object sender, EventArgs e) {
        Application.Exit();
    }

    private void buttonExecute_Click(object sender, EventArgs e) {
        if (textBoxQuery.Text == "")
            return;
        try {
            XmlNodeList nodeList = mCurrentNode.SelectNodes(textBoxQuery.Text);
            ClearListBox();
            DisplayList(nodeList);
        catch (System.Exception err) {
            MessageBox.Show(err.Message);
        }
    }

    private void radioButtonSelectAllAuthors_CheckedChanged(object sender, EventArgs e) {
        XmlNodeList nodeList = mCurrentNode.SelectNodes("//book/author");
        ClearListBox();
        DisplayList(nodeList);
    }

    private void radioButtonSelectSpecificAuthor_CheckedChanged(object sender, EventArgs e) {
        XmlNodeList nodeList = mCurrentNode.SelectNodes("//book[author='J']");
        ClearListBox();
        DisplayList(nodeList);
    }

    private void radioButtonSelectAllBooks_CheckedChanged(object sender, EventArgs e) {
        XmlNodeList nodeList = mCurrentNode.SelectNodes("//book");
        ClearListBox();
        DisplayList(nodeList);
    }

    private void radioButtonSetBookAsCurrent_CheckedChanged(object sender, EventArgs e) {
        mCurrentNode = mCurrentNode.SelectSingleNode("book[title='C#']");
        ClearListBox();
        RecurseXmlDocumentNoSiblings(mCurrentNode, 0);
    }

    private void radioButtonSetBooksAsCurrent_CheckedChanged(object sender, EventArgs e) {
        mCurrentNode = mCurrentNode.SelectSingleNode("//books");
        ClearListBox();
        RecurseXmlDocumentNoSiblings(mCurrentNode, 0);
    }

    private void radioButtonSelectAllChildren_CheckedChanged(object sender, EventArgs e) {
        XmlNodeList nodeList = mCurrentNode.SelectNodes("*");
        ClearListBox();
        DisplayList(nodeList);
    }
    private void InitializeComponent() {
        this.radioButtonSelectRoot = new System.Windows.Forms.RadioButton();
        this.radioButtonSelectAllChildren = new System.Windows.Forms.RadioButton();
        this.radioButtonSetBooksAsCurrent = new System.Windows.Forms.RadioButton();
        this.radioButtonSetBookAsCurrent = new System.Windows.Forms.RadioButton();
        this.radioButtonSelectAllBooks = new System.Windows.Forms.RadioButton();
        this.radioButtonSelectSpecificAuthor = new System.Windows.Forms.RadioButton();
        this.radioButtonSelectAllAuthors = new System.Windows.Forms.RadioButton();
        this.textBoxQuery = new System.Windows.Forms.TextBox();
        this.buttonExecute = new System.Windows.Forms.Button();
        this.buttonClose = new System.Windows.Forms.Button();
        this.listBoxResult = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        this.radioButtonSelectRoot.AutoSize = true;
        this.radioButtonSelectRoot.Location = new System.Drawing.Point(13234);
        this.radioButtonSelectRoot.Size = new System.Drawing.Size(7217);
        this.radioButtonSelectRoot.Text = "Select root";
        this.radioButtonSelectRoot.CheckedChanged += new System.EventHandler(this.radioButtonSelectRoot_CheckedChanged);

        this.radioButtonSelectAllChildren.AutoSize = true;
        this.radioButtonSelectAllChildren.Location = new System.Drawing.Point(163282);
        this.radioButtonSelectAllChildren.Size = new System.Drawing.Size(10417);
        this.radioButtonSelectAllChildren.Text = "Select all children";
        this.radioButtonSelectAllChildren.CheckedChanged += new System.EventHandler(this.radioButtonSelectAllChildren_CheckedChanged);

        this.radioButtonSetBooksAsCurrent.AutoSize = true;
        this.radioButtonSetBooksAsCurrent.Location = new System.Drawing.Point(163258);
        this.radioButtonSetBooksAsCurrent.Size = new System.Drawing.Size(12017);
        this.radioButtonSetBooksAsCurrent.Text = "Set Books as current";
        this.radioButtonSetBooksAsCurrent.CheckedChanged += new System.EventHandler(this.radioButtonSetBooksAsCurrent_CheckedChanged);

        this.radioButtonSetBookAsCurrent.AutoSize = true;
        this.radioButtonSetBookAsCurrent.Location = new System.Drawing.Point(163234);
        this.radioButtonSetBookAsCurrent.Size = new System.Drawing.Size(11517);
        this.radioButtonSetBookAsCurrent.Text = "Set Book as current";
        this.radioButtonSetBookAsCurrent.CheckedChanged += new System.EventHandler(this.radioButtonSetBookAsCurrent_CheckedChanged);

        this.radioButtonSelectAllBooks.AutoSize = true;
        this.radioButtonSelectAllBooks.Location = new System.Drawing.Point(13306);
        this.radioButtonSelectAllBooks.Size = new System.Drawing.Size(9617);
        this.radioButtonSelectAllBooks.Text = "Select all books";
        this.radioButtonSelectAllBooks.CheckedChanged += new System.EventHandler(this.radioButtonSelectAllBooks_CheckedChanged);

        this.radioButtonSelectSpecificAuthor.AutoSize = true;
        this.radioButtonSelectSpecificAuthor.Location = new System.Drawing.Point(13282);
        this.radioButtonSelectSpecificAuthor.Size = new System.Drawing.Size(13717);
        this.radioButtonSelectSpecificAuthor.Text = "Select by specific author";
        this.radioButtonSelectSpecificAuthor.CheckedChanged += new System.EventHandler(this.radioButtonSelectSpecificAuthor_CheckedChanged);

        this.radioButtonSelectAllAuthors.AutoSize = true;
        this.radioButtonSelectAllAuthors.Location = new System.Drawing.Point(13258);
        this.radioButtonSelectAllAuthors.Margin = new System.Windows.Forms.Padding(3133);
        this.radioButtonSelectAllAuthors.Size = new System.Drawing.Size(10217);
        this.radioButtonSelectAllAuthors.Text = "Select all authors";
        this.radioButtonSelectAllAuthors.CheckedChanged += new System.EventHandler(this.radioButtonSelectAllAuthors_CheckedChanged);

        this.textBoxQuery.Location = new System.Drawing.Point(13330);
        this.textBoxQuery.Size = new System.Drawing.Size(38520);

        this.buttonExecute.Location = new System.Drawing.Point(323234);
        this.buttonExecute.Text = "Execute";
        this.buttonExecute.Click += new System.EventHandler(this.buttonExecute_Click);

        this.buttonClose.Location = new System.Drawing.Point(40513);
        this.buttonClose.Text = "Close";
        this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);

        this.listBoxResult.FormattingEnabled = true;
        this.listBoxResult.Location = new System.Drawing.Point(1313);
        this.listBoxResult.Size = new System.Drawing.Size(385212);

        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(492362);
        this.Controls.Add(this.listBoxResult);
        this.Controls.Add(this.buttonClose);
        this.Controls.Add(this.buttonExecute);
        this.Controls.Add(this.textBoxQuery);
        this.Controls.Add(this.radioButtonSelectAllAuthors);
        this.Controls.Add(this.radioButtonSelectSpecificAuthor);
        this.Controls.Add(this.radioButtonSelectAllBooks);
        this.Controls.Add(this.radioButtonSetBookAsCurrent);
        this.Controls.Add(this.radioButtonSetBooksAsCurrent);
        this.Controls.Add(this.radioButtonSelectAllChildren);
        this.Controls.Add(this.radioButtonSelectRoot);

        this.Text = "XPath Queries";
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.RadioButton radioButtonSelectRoot;
    private System.Windows.Forms.RadioButton radioButtonSelectAllChildren;
    private System.Windows.Forms.RadioButton radioButtonSetBooksAsCurrent;
    private System.Windows.Forms.RadioButton radioButtonSetBookAsCurrent;
    private System.Windows.Forms.RadioButton radioButtonSelectAllBooks;
    private System.Windows.Forms.RadioButton radioButtonSelectSpecificAuthor;
    private System.Windows.Forms.RadioButton radioButtonSelectAllAuthors;
    private System.Windows.Forms.TextBox textBoxQuery;
    private System.Windows.Forms.Button buttonExecute;
    private System.Windows.Forms.Button buttonClose;
    private System.Windows.Forms.ListBox listBoxResult;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

}



           
       
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.  XPath Expression Syntax
w___w__w._j__a__v_a_2__s.___co_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.