XPathDocument的相关用法(待整理)

<?xml version="1.0" encoding="utf-8"?>
<notepad>
<topic>主题</topic>
<content>内容内容</content>
</notepad>

xpath.aspx
<%@Page Language="c#" ValidateRequest="false" Debug="true"%>
<%@Import Namespace="System.Xml.XPath"%>
<Script Language="C#" Runat="Server">
public void Page_Load(Object src,EventArgs e)
{
    XPathDocument doc = new XPathDocument(Server.MapPath("demo.xml"));
    XPathNavigator nav = doc.CreateNavigator();
    XPathNodeIterator ite = nav.Select("/notepad/topic");
    //移动到下一个节点,下一个节点是文字节点。
    ite.MoveNext();
    Response.Write (ite.Current.Value);
    ite = nav.Select("/notepad/content");
    //移动到下一个节点,下一个节点是文字节点。
    ite.MoveNext();
    Response.Write (ite.Current.Value);
}
</Script>

 

 

 

 

 

 

C#

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace convertDell
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Convertbutton_Click(object sender, EventArgs e)
        {
           // char[] Oldchar = {'?', '3', '2', '4'};
            string[] OldString;
            XPathDocument XPathDom;
            XPathNavigator XPathNav;
            XPathNodeIterator XPathNodeIte;
            string ExpressionStr,Newchar;
            ExpressionStr = string.Empty;
            Newchar = string.Empty;
            //OldString = string.Empty;
            OldString = OldtextBox.Text.Split(new char[]{'/r','/n'},StringSplitOptions.RemoveEmptyEntries);
            XPathDom=new XPathDocument("ConvertRull.xml");
            XPathNav = XPathDom.CreateNavigator();
           
            foreach (string Old in OldString)
            {
                ExpressionStr = "convertrull/rull1/word[@old='" + Old + "']";
                XPathNodeIte = XPathNav.Select(ExpressionStr);
                /*
                while (XPathNodeIte.MoveNext())
                    Newchar += XPathNodeIte.Current.Value + "/r/n";
                 */
                XPathNodeIte.MoveNext();
                //移动到下一个结点,下一个结点才是文字结点,当前结点是元素结点,若没往下移,取出来的
                //值则是元素的值,若元素有好多个相同,则值就好多个,即去掉MoveNext()这行代码
                Newchar += XPathNodeIte.Current.Value + "/r/n";
            }
            ConverttotextBox.Text = Newchar;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值