Xml encapsulation and parsing (in javascript and C #) _ javascript skills

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

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.