码迷,mamicode.com
首页 > 其他好文 > 详细

XPath相关笔记

时间:2016-07-08 17:57:57      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

<?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>
=================================================================

XPath相关笔记

标签:

原文地址:http://www.cnblogs.com/blogLYF/p/5653954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!