首页在VS2010 中新建一个WEBservice程序, 源码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Xml; using System.Xml.XPath; /// <summary> ///WebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string GetXMLByXpath(string xpaths) { XPathDocument xmldoc = new XPathDocument(Server.MapPath("books.xml")); XPathNavigator xpathnav = xmldoc.CreateNavigator(); XPathNodeIterator result; result = xpathnav.Select(xpaths); string ss=""; while (result.MoveNext()) { ss += result.Current.InnerXml; //ss += "< br />"; } if (ss.Length<1) return "没有符号要求的结果"; else return ss; } } 在新建一个html文件 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <mce:script src="Scripts/jquery-1.4.1-vsdoc.js" mce_src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></mce:script> <mce:script type="text/javascript"><!-- $(document).ready(function () { $("#sent").click(function () { $.ajax({ url: "WebService.asmx/GetXMLByXpath", async: false, type: "POST", data: "xpaths="+$("#xpathcommand").attr("value"), dataType: "html", success: function (result) { // var aa = eval(result); $("#result").html(result); } }) }) }); // --></mce:script> </head> <body> <div style="border: thin solid #0000FF; float:left; display:block; width:55%; height:100%; "> <div id="commandarea"> <input id="xpathcommand" type="text" value="input" /> <input type="button" id="sent" value="sent" /> </div> <div id="result"> </div></div> <div style="border: thin solid #0000FF; width:40%; float:left;height:100%;"> <iframe src="books.xml" mce_src="books.xml" width="100%"; height="550px";scrolling="auto" /> </div> </body> </html> books.xml 代码, <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="eng">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> 运行结果 源文件下载