_javascript techniques in XML encapsulation and parsing (JavaScript and C #)

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.