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:
function test () { var xmlstring = "<bookstore>" + "<book type= ' compulsory course ' isbn= ' 7-111-19149-2 ' >" + " <title> Data Structures </title> "+ " <author> Min </author> "+ " <price>30.00</price > "+ " </book></bookstore> "; $.ajax ({type: "post", url: "Hand/ajax.ashx", Data: "Strxml=" +xmlstring,datatype: "xml", Success:function (XML) {// Update the page alert ("Success") according to Resulttext, 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:
XmlDocument Xdoc = new XmlDocument ();
XML string Manipulation Xdoc. LOADXML (strxml);//Read XML string Strxml
Add a price element. Adds a node XmlElement Newelem = doc. CreateElement ("price"); Newelem.innertext = "10.95"; Doc. Documentelement.appendchild (Newelem);//Add a node
Xdoc. Load (filename);//Read XML file filename is the path to the file
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
Get the root node bookstorexmlnode xn = xdoc. selectSingleNode ("Bookstore");//Get all child nodes of the root node xmlnodelist xnl = xn. childnodes;//converts a node to an element, making it easy to get the attribute value of a node XmlElement XE = (XmlElement) (XNL. Item (0));//Get the property value of type and ISBN two properties string bookisbn = Xe. GetAttribute ("ISBN"). ToString (); string booktype = Xe. GetAttribute ("Type"). ToString ();//Get all the child nodes of the book node XmlNodeList xnl0 = Xe. Childnodes;string bookname = xnl0. Item (0). Innertext;string Bookauthor = xnl0. Item (1). Innertext;double Bookprice = convert.todouble (xnl0. Item (2). InnerText);
After the background processing, return XML format data, of course this premise context. Response.ContentType = "Text/xml";
DataSet ds = new DataSet ();d s = GetList (); context. Response.Clear (); context. Response.Write (ds. GETXML ());
Private DataSet GetList () { DataSet ds = new DataSet (); DataTable dt = new DataTable (); 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); return DS;}
This is Jquery+ajax+xml's application.
Ajax implementation Asynchronous Operation instance _ request data for XML format