• 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>
    =================================================================
  • 相关阅读:
    apache和tomcat有什么不同,为什么要整合apache 和tomcat
    使用jstack分析cpu消耗过高的问题
    eclipse建立springMVC 简单项目
    JAVA中的反射机制
    MySQL索引的Index method中btree和hash的优缺点
    JAVA中的线程安全与非线程安全
    Java中普通代码块,构造代码块,静态代码块执行顺序
    jvm的内部体系结构浅析
    CPU思考
    linux内核调度算法(2)--CPU时间片如何分配 转!
  • 原文地址:https://www.cnblogs.com/blogLYF/p/5653954.html
Copyright © 2020-2023  润新知