Xml encapsulation and parsing of the implementation code in javascript and C # respectively.
1. xml parsing (in javascript ):
The specific code is as follows. The root of the parsed result is the Dom tree.
The Code is as follows:
If (window. ActiveXObject ){
Var doc = new ActiveXObject ("Microsoft. XMLDOM ");
Doc. async = "false ";
Doc. loadXML (strXml );
} Else {
Var parser = new DOMParser ();
Var doc = parser. parseFromString (strXml, "text/xml ");
}
Var root = doc.doc umentElement;
2. xml encapsulation (in javascript ):
(This Code encapsulates the table on the page as an xml file)
The Code is as follows:
Var xmlDoc = new ActiveXObject ("Microsoft. XMLDOM ");
XmlDoc. loadXML (" ");
Var root = xmlDoc.doc umentElement;
For (var index = 0; index {
Var row = xmlDoc. createElement ("Row ");
For (var colIndex = 0; colIndex {
Var currentCell = this. table. rows [index]. cells [colIndex];
Var cell = xmlDoc. createElement ("Cell ");
Cell. setAttribute ("Name", this. table. columns [colIndex]. id );
Cell. setAttribute ("Value", currentCell. value );
Row. appendChild (cell );
}
Root. appendChild (row );
}
For how to transmit xml from the front-end to the back-end of ajax implementation, refer to jquery for xml frontend and back-end transmission.
3. xml encapsulation: (C #)
The specific method is as follows,
The Code is as follows:
XmlDocument doc = new XmlDocument ();
Doc. LoadXml ("");
XmlElement root = doc. DocumentElement;
Root. SetAttribute ("Name", name); // here name assigns a Name attribute for the xml
Foreach (ListObject Object in ListResult) // listResult is a list table composed of listObject objects. The object is an element of listResult, which is in the ListObject type.
{
XmlElement item = doc. CreateElement ("Item ");
Item. SetAttribute ("Key", Object. key); // the key and value are the attribute elements of the Object.
Item. SetAttribute ("Value", Object. Value );
Root. AppendChild (item );
}
The last generated root is xml.
4. xml parsing (c #)
The Code is as follows:
XmlDocument doc = new XmlDocument ();
Try
{
Doc. Load (Request. InputStream); // Load the xml stream of the request.
}
Catch (Exception e)
{}
XmlNodeList rowList;
RowList = doc. DocumentElement. SelectNodes ("Row ");
List VoList = new List (RowList. Count); // initialize a List. Change the element in the list to an ObjectVO object.
Foreach (XmlNode row in rowList)
{
ObjectVO VO = new ObjectVO ();
VO. VOElement1 = Convert. toInt32 (row. selectSingleNode ("Cell [@ Name = 'voelement1 ']") as XmlElement ). getAttribute ("Value"); // element VOElement1 in vo is int type
VO. VOElement2 = (row. selectSingleNode ("Cell [@ Name = 'voelement2']") as XmlElement ). getAttribute ("Value "). toString (); // or the value of the value Attribute whose name is VOElement2 in the cell element in xml
VO. VOElement3 = (row. SelectSingleNode ("Cell [@ Name = 'voelement3']") as XmlElement). GetAttribute ("Value"). ToString ();
VoList. Add (VO );
}
Return voList;