zoukankan      html  css  js  c++  java
  • 利用XPath查找XML文档中的信息

    样例所用的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>
        
    }
    
    
    
  • 相关阅读:
    关于sharepoint组里面有哪些成员
    如何在ProjectServer用代码修改用户属性?
    Project Server的Psi汇总
    Sharepoint Two Webpart Connect
    TQ6410_V3 wince6.0系统 调试口改普通串口方法
    流方式文件读写(简单实现)
    Django 框架请求响应流程图
    Silverlight项目基本文件结构
    Ext.DomHelper类的使用示例(内容操作)
    利用urllib2获取请求头部信息
  • 原文地址:https://www.cnblogs.com/lilin/p/1718058.html
Copyright © 2011-2022 走看看