1.xml parsing (in JavaScript):
The concrete code is as follows, the parsed result is the DOM tree.
Copy Code code 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.documentelement;
2.xml Encapsulation (JavaScript):
(This code encapsulates a table in the page as an XML)
Copy Code code as follows:
var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.loadxml ("<Rows></Rows>");
var root = xmldoc.documentelement;
for (Var index=0;index<this.table.rows.length;index++)
{
var row = xmldoc.createelement ("row");
for (var colindex = 0;colindex<this.table.rows[index].cells.length;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 AJAX implementation of the foreground XML to the background of the transfer can refer to jquery implementation of XML front-end transmission.
3.xml Encapsulation: (C #)
The specific methods are as follows
Copy Code code as follows:
XmlDocument doc = new XmlDocument ();
Doc. Loadxml ("<Data></Data>");
XmlElement root = Doc. DocumentElement;
Root. SetAttribute ("name", name);//Here name assigns a name attribute to the XML
foreach (ListObject object in Listresult)//Listresult is a list table of ListObject objects, where object is an element of Listresult, He's a ListObject type.
{
XmlElement item = doc. CreateElement ("Item");
Item. SetAttribute ("Key", object.key);//the property element in which Key,value is an Object, respectively
Item. SetAttribute ("Value", Object.value);
Root. AppendChild (item);
}
The last generated root is XML.
4.xml Parsing (C #)
Copy Code code as follows:
XmlDocument doc = new XmlDocument ();
Try
{
Doc. Load (request.inputstream);//This loads the XML stream
}
catch (Exception e)
{}
XmlNodeList rowlist for request requests;
Rowlist = doc. Documentelement.selectnodes ("Row");
list<objectvo> volist = new list<objectvo> (rowlist.count);//Initialize a list, and the constituent elements in the list are Objectvo objects
foreach (XmlNode row in rowlist)
{
Objectvo VO = new Objectvo ();
VO. VOElement1 = Convert.ToInt32 (row. selectSingleNode ("cell[@Name = ' VOElement1 ']") as XmlElement). GetAttribute ("Value"));//vo element VOElement1 is int
VO. VOElement2 = (row. selectSingleNode ("cell[@Name = ' VOElement2 ']") as XmlElement). GetAttribute ("Value"). ToString ();//or takes the value of the Value property named VOElement2 in the cell element in the XML
VO. VOElement3 = (row. selectSingleNode ("cell[@Name = ' VOElement3 ']") as XmlElement). GetAttribute ("Value"). ToString ();
Volist.add (VO);
}
Return volist;