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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
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
8.Using XPath to get string value, integer value and boolean value
9.Extensions.XPathSelectElement selects an XElement using a XPath expression
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
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.