zoukankan      html  css  js  c++  java
  • XPath相关笔记

    <?xml version="1.0" encoding="utf-8" ?>
    <employees>
      <employee on="01">
        <name>Tom</name>
        <sex>男</sex>
        <birth>
          <year>1982</year>
          <month>5</month>
          <day>22</day>
        </birth>
        <sales>
          <year>2010</year>
          <month>1</month>
          <sale>320000</sale>
        </sales>
        <salary>8600</salary>
      </employee>
      
      <employee on="02">
        <name>Jack</name>
        <sex>男</sex>
        <birth>
          <year>1981</year>
          <month>7</month>
          <day>10</day>
        </birth>
        <sales>
          <year>2010</year>
          <month>1</month>
          <sale>300000</sale>
        </sales>
        <salary>6000</salary>
      </employee>
      <employee on="03">
        <name>Jennifer</name>
        <sex>女</sex>
        <birth>
          <year>1982</year>
          <month>6</month>
          <day>22</day>
        </birth>
        <sales>
          <year>2010</year>
          <month>1</month>
          <sale>500000</sale>
        </sales>
        <salary>7000</salary>
      </employee>
      
    </employees>
    =============================================================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml.XPath;
    namespace WebApplication
    {
        public partial class XPathNav : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            protected void btnNav_Click(object sender, EventArgs e)
            {
                //创建XPathDocument对象
                XPathDocument xdoc = new XPathDocument(Server.MapPath("~/employees.xml")); 
                //创建Xpath导航
                XPathNavigator xnav = xdoc.CreateNavigator();
                //装入迭代器(结果)
                XPathNodeIterator iterator = xnav.Select("descendant::employee");  
                string outPut = "";
                while(iterator.MoveNext()){
                    outPut += "员工:" + iterator.Current.SelectSingleNode("name").Value+"<br/>"; //在当前节点下查找单一的节点name的值
                    outPut += " " + "工资:" + iterator.Current.SelectSingleNode("salary").Value + "<br/>"; // 在当前节点下查找单一的节点salary的值
                    outPut += "<hr>";
                }
                outPut += "<hr>";
                Int32 avgSal = (Int32)(Double)xnav.Evaluate("sum(//employee/salary) div count(//employee)");
                outPut += "平均工资:¥"+avgSal.ToString();
                this.divOut.InnerHtml = outPut;
            }
        }
    }
    ===================================================================
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XPathNav.aspx.cs" Inherits="WebApplication.XPathNav" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
             <asp:Button runat="server" ID="btnNav" Text="Navigate" OnClick="btnNav_Click"  />
             <div runat="server" id="divOut">
             </div>
        </div>
        </form>
    </body>
    </html>
    =================================================================
  • 相关阅读:
    OSG-提示“error reading file e:1.jpg file not handled”
    OSG-加载地球文件报0x00000005错误,提示error reading file simple.earth file not handled
    QT-找开工程后,最上方提示the code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
    我的书《Unity3D动作游戏开发实战》出版了
    java中无符号类型的第三方库jOOU
    Windows批处理备份mysql数据
    使用 DevTools 时,通用Mapper经常会出现 class x.x.A cannot be cast to x.x.A
    Java版本,Java版本MongoDB驱动,驱动与MongoDB数据库,Spring之间的兼容性
    Jrebel本地激活方法
    wget下载指定网站目录下的所有内容
  • 原文地址:https://www.cnblogs.com/blogLYF/p/5653954.html
Copyright © 2011-2022 走看看