JS Classification has a section "Native JS asynchronous request, XML parsing" mainly explains how JS foreground processing XML format request and how to accept the XML data returned by the server parsing, today I will use an example to illustrate how to do.
The parameter type of the foreground is also XML using jquery:
| 123456789101112131415161718192021 |
function test(){ varxmlString ="<bookstore>"+ "<book Type=‘必修课‘ ISBN=‘7-111-19149-2‘>"+ "<title>数据结构</title>"+ "<author>严蔚敏</author>"+ "<price>30.00</price>"+ "</book></bookstore>"; $.ajax({ type: "post", url: "Hand/Ajax.ashx", data: "strxml="+xmlString, datatype:"xml", success: function(xml){ //根据resultText更新页面 alert("success"); alert($(xml).find(‘Table1‘).find(‘title‘).text()); }, error:function(XMLResponse){alert(XMLResponse.responseText)}});} |
The foreground is in the XML format parameters, how to do in the background? This is for XML read-write, here is a simple explanation:
| 12 |
XmlDocument xdoc = newXmlDocument();<br>//xml字符串操作xdoc.LoadXml(strxml);//读取xml字符串strxml |
Add a price element. Adds a node XmlElement Newelem = doc. CreateElement ("price"); Newelem.innertext = "10.95"; Doc. Documentelement.appendchild (Newelem);//Add a node
| 1 |
xdoc.Load(fileName);//读取xml文件fileName是文件的路径 |
The above simple description of Loadxml and load simple usage, here do not elaborate. The following is the XML format parameter for background processing foreground
| 1234567891011121314 |
// 得到根节点bookstoreXmlNode xn = xdoc.SelectSingleNode("bookstore");// 得到根节点的所有子节点XmlNodeList xnl = xn.ChildNodes;// 将节点转换为元素,便于得到节点的属性值XmlElement xe = (XmlElement)(xnl.Item(0));// 得到Type和ISBN两个属性的属性值stringbookISBN = xe.GetAttribute("ISBN").ToString();stringbookType = xe.GetAttribute("Type").ToString();// 得到Book节点的所有子节点XmlNodeList xnl0 = xe.ChildNodes;stringbookName = xnl0.Item(0).InnerText;stringbookAuthor = xnl0.Item(1).InnerText;doublebookPrice = Convert.ToDouble(xnl0.Item(2).InnerText); |
After the background processing, return XML format data, of course this premise context. Response.ContentType = "Text/xml";
| 1234 |
DataSet ds = newDataSet();ds = GetList();context.Response.Clear();context.Response.Write(ds.GetXml()); |
| 123456789101112131415 |
privateDataSet GetList(){ DataSet ds = newDataSet(); DataTable dt = newDataTable(); dt.Columns.Add("title"); dt.Columns.Add("author"); dt.Columns.Add("price"); DataRow dr = dt.NewRow(); dr["title"] = "book1"; dr["author"] = "matest"; dr["price"] = 30.01; dt.Rows.Add(dr); ds.Tables.Add(dt); returnds;} |
This is Jquery+ajax+xml's application.
Ajax implementation Asynchronous Operation instance _ request data for XML format