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(13, 234);
this.radioButtonSelectRoot.Size = new System.Drawing.Size(72, 17);
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(163, 282);
this.radioButtonSelectAllChildren.Size = new System.Drawing.Size(104, 17);
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(163, 258);
this.radioButtonSetBooksAsCurrent.Size = new System.Drawing.Size(120, 17);
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(163, 234);
this.radioButtonSetBookAsCurrent.Size = new System.Drawing.Size(115, 17);
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(13, 306);
this.radioButtonSelectAllBooks.Size = new System.Drawing.Size(96, 17);
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(13, 282);
this.radioButtonSelectSpecificAuthor.Size = new System.Drawing.Size(137, 17);
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(13, 258);
this.radioButtonSelectAllAuthors.Margin = new System.Windows.Forms.Padding(3, 1, 3, 3);
this.radioButtonSelectAllAuthors.Size = new System.Drawing.Size(102, 17);
this.radioButtonSelectAllAuthors.Text = "Select all authors";
this.radioButtonSelectAllAuthors.CheckedChanged += new System.EventHandler(this.radioButtonSelectAllAuthors_CheckedChanged);
this.textBoxQuery.Location = new System.Drawing.Point(13, 330);
this.textBoxQuery.Size = new System.Drawing.Size(385, 20);
this.buttonExecute.Location = new System.Drawing.Point(323, 234);
this.buttonExecute.Text = "Execute";
this.buttonExecute.Click += new System.EventHandler(this.buttonExecute_Click);
this.buttonClose.Location = new System.Drawing.Point(405, 13);
this.buttonClose.Text = "Close";
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
this.listBoxResult.FormattingEnabled = true;
this.listBoxResult.Location = new System.Drawing.Point(13, 13);
this.listBoxResult.Size = new System.Drawing.Size(385, 212);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(492, 362);
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());
}
}