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(){
var
xmlString =
"<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 = new XmlDocument();<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 |
// 得到根节点bookstore
XmlNode xn = xdoc.SelectSingleNode(
"bookstore"
);
// 得到根节点的所有子节点
XmlNodeList xnl = xn.ChildNodes;
// 将节点转换为元素,便于得到节点的属性值
XmlElement xe = (XmlElement)(xnl.Item(0));
// 得到Type和ISBN两个属性的属性值
string
bookISBN = xe.GetAttribute(
"ISBN"
).ToString();
string
bookType = xe.GetAttribute(
"Type"
).ToString();
// 得到Book节点的所有子节点
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";
1234 |
DataSet ds = new DataSet(); ds = GetList(); context.Response.Clear(); context.Response.Write(ds.GetXml()); |
123456789101112131415 |
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