导航

利用XPath查找XML文档中的信息

Posted on 2010-04-22 15:15  lilin  阅读(545)  评论(0编辑  收藏  举报

样例所用的Xml文件:books.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>
</bookstore>

运行下面的函数

using System.Text;
using System.Xml;

//参考:http://www.w3school.com.cn/xpath/xpath_intro.asp
class Program
{
    static void Main(string[] args)
    {
        SelectTitleYearNodes();
    }

    //利用XPath查找XML文档中的信息
    static void SelectNodes()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("E:\\books.xml");

        //倒数第二个book元素
        XmlNodeList nodes = doc.SelectNodes("/bookstore/book[last()-1]");
        string text;
        foreach (XmlNode node in nodes)
        {
            text = node.OuterXml;
        }
    }
    //输出结果
    //<book category="WEB">
    //  <title lang="en">XQuery Kick Start</title>
    //  <author>James McGovern</author>
    //  <author>Per Bothner</author>
    //  <author>Kurt Cagle</author>
    //  <author>James Linn</author>
    //  <author>Vaidyanathan Nagarajan</author>
    //  <year>2003</year>
    //  <price>49.99</price>
    //</book>


    static void SelectPositionNodes()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("E:\\books.xml");

        //最前面的两个book元素集
        XmlNodeList nodes = doc.SelectNodes("/bookstore/book[position()<3]");
        StringBuilder sb = new StringBuilder();
        string text;
        foreach (XmlNode node in nodes)
        {
            text = node.OuterXml;
            sb.Append(text);
        }
    }
    //输出结果
    //<book category="COOKING">
    //  <title lang="en">Everyday Italian</title>
    //  <author>Giada De Laurentiis</author>
    //  <year>2005</year>
    //  <price>30.00</price>
    //</book>
    //<book category="CHILDREN">
    //  <title lang="en">Harry Potter</title>
    //  <author>J K. Rowling</author>
    //  <year>2005</year>
    //  <price>29.99</price>
    //</book>



    static void SelectPriceNodes()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("E:\\books.xml");

        //price元素的值须大于35.00的book元素集
        XmlNodeList nodes = doc.SelectNodes("/bookstore/book[price>35.00]");
        StringBuilder sb = new StringBuilder();
        string text;
        foreach (XmlNode node in nodes)
        {
            text = node.OuterXml;
            sb.Append(text);
        }
    }
    //输出结果
    //<book category="WEB">
    //  <title lang="en">XQuery Kick Start</title>
    //  <author>James McGovern</author>
    //  <author>Per Bothner</author>
    //  <author>Kurt Cagle</author>
    //  <author>James Linn</author>
    //  <author>Vaidyanathan Nagarajan</author>
    //  <year>2003</year>
    //  <price>49.99</price>
    //</book>
    //<book category="WEB">
    //  <title lang="en">Learning XML</title>
    //  <author>Erik T. Ray</author>
    //  <year>2003</year>
    //  <price>39.95</price>
    //</book>


    static void SelectTitleNodes()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("E:\\books.xml");

        //price元素的值须大于35.00的title元素集
        XmlNodeList nodes = doc.SelectNodes("/bookstore/book[price>35.00]/title");
        StringBuilder sb = new StringBuilder();
        string text;
        foreach (XmlNode node in nodes)
        {
            text = node.OuterXml;
            sb.Append(text);
        }
    }
    //输出结果
    //<title lang="en">XQuery Kick Start</title>
    //<title lang="en">Learning XML</title>


    static void SelectTitleYearNodes()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("E:\\books.xml");

        //选取所有book元素的title和year元素
        XmlNodeList nodes = doc.SelectNodes("/bookstore/book/title | /bookstore/book/year");
        StringBuilder sb = new StringBuilder();
        string text;
        foreach (XmlNode node in nodes)
        {
            text = node.OuterXml;
            sb.Append(text);
        }
    }
    //输出结果
    //<title lang="en">Everyday Italian</title>
    //<year>2005</year>
    //<title lang="en">Harry Potter</title>
    //<year>2005</year>
    //<title lang="en">XQuery Kick Start</title>
    //<year>2003</year>
    //<title lang="en">Learning XML</title>
    //<year>2003</year>
    
}