<?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; } } }