检索xml节点
来源:互联网 发布:厦门软件企业名录 编辑:程序博客网 时间:2024/05/24 05:02
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Xml;namespace SelectXmlDemo{ public partial class Form1 : Form { XmlDocument doc = new XmlDocument(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { doc.Load(Application.StartupPath + "//books.xml"); } //得到根结点 private void button2_Click(object sender, EventArgs e) { XmlNode root=doc.SelectSingleNode("/books"); this.textBox1.Text = root.InnerXml; } //得到所有的book private void button3_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("/books/book"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //得到所有作者 private void button4_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("/books/book/author"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //如果路径以双斜线 // 开头, 则表示选择文档中所有满足双斜线//之后规则的元素(无论层级关系) private void button5_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//author"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //选择所有父元素是book的author元素 private void button6_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//book/author"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //星号 * 表示选择所有由星号之前的路径所定位的元素 private void button7_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//book[@id='bk101']/*"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //得到深度为第三层的作者//第二层,第四层都不选 private void button8_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("/*/*/author"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //方块号里的表达式可以进一步的指定元素, 其中数字表示元素在选择集里的位置, 而last()函数则表示选择集中的最后一个元素. private void button9_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("/books/book[2]"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //得到最后一个节点 private void button10_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("/books/book[last()]"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); } //得到所有有id属性的节点 private void button11_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//@id"); foreach (XmlNode node in nodes) { // this.textBox1.Text += node.Value + "/r/n/r/n/r/n"; this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //选择有任意属性的book元素 private void button12_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//book[@id]"); foreach (XmlNode node in nodes) { // this.textBox1.Text += node.Value + "/r/n/r/n/r/n"; this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //得到所没有id属性的节点 private void button13_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//book[not(@id)]"); foreach (XmlNode node in nodes) { // this.textBox1.Text += node.Value + "/r/n/r/n/r/n"; this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //根据属性id得到节点 private void button14_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//book[@id='bk101']"); foreach (XmlNode node in nodes) { // this.textBox1.Text += node.Value + "/r/n/r/n/r/n"; this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //去空格得到节点 private void button15_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//book[normalize-space(@id)='bk106']"); // XmlNodeList nodes = doc.SelectNodes("//book[@id='bk106']"); foreach (XmlNode node in nodes) { // this.textBox1.Text += node.Value + "/r/n/r/n/r/n"; this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //descendant (后代)轴包含上下文节点的后代,一个后代是指子节点或者子节点的子节点等等, 因此descendant轴不会包含属性和命名空间节点. private void button16_Click(object sender, EventArgs e) { XmlNode book = doc.SelectSingleNode("descendant::book[author='Cores,Eva']"); this.textBox1.Text = book.InnerXml; } ////book/descendant::title //得到所有以book为祖先的title节点 private void button17_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//book/descendant::title"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //多条件查询 private void button18_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("/books/book[@id='bk101' and author='Gambardella,Matthew']"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } //parent轴(axis)包含上下文节点的父节点, 如果有父节点的话 private void button19_Click(object sender, EventArgs e) { XmlNodeList nodes = doc.SelectNodes("//author/parent::*"); foreach (XmlNode node in nodes) { this.textBox1.Text += node.InnerXml + "/r/n/r/n/r/n"; } } }}