Home  >  Article  >  Web Front-end  >  xml encapsulation and parsing (in javascript and C#)_javascript skills

xml encapsulation and parsing (in javascript and C#)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:49:071089browse
1.xml parsing (in javascript):
The specific code is as follows. The root of the parsed result is the Dom tree.
Copy code 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.documentElement;

2.xml encapsulation ( javascript):
(This code encapsulates the table in the page into an xml)
Copy code Code As follows:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML("");
var root = xmlDoc.documentElement;
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 the ajax implementation of the transmission of front-end xml to the back-end, you can refer to the jquery implementation Front-end and back-end transmission of xml.
3.xml encapsulation: (C#)
The specific method is as follows,
Copy code 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 to the xml
foreach (ListObject Object in ListResult)//where listResult is a list table composed of listObject objects , where object is an element of listResult, which is of type ListObject
{
XmlElement item = doc.CreateElement("Item");
item.SetAttribute("Key", Object.key);/ /where key and value are the attribute elements of Object respectively
item.SetAttribute("Value", Object.Value);
root.AppendChild(item);
}

The final generated root is xml.
4.xml parsing (c#)
Copy code The code is as follows:

XmlDocument doc = new XmlDocument();
try
{
doc.Load(Request.InputStream);//Load the request xml here Stream
}
catch (Exception e)
{}
XmlNodeList rowList;
rowList = doc.DocumentElement.SelectNodes("Row");
List voList = new List(rowList.Count);//Initialize a List and change the elements in the list to ObjectVO objects
foreach (XmlNode row in rowList)
{
ObjectVO VO = new ObjectVO();
VO.VOElement1 = Convert.ToInt32((row.SelectSingleNode("Cell[@Name='VOElement1']") as XmlElement).GetAttribute("Value"));//The element VOElement1 in vo is of type int
VO.VOElement2 = (row.SelectSingleNode("Cell[@Name='VOElement2']") as XmlElement).GetAttribute("Value").ToString();//or take the cell element in xml whose name is VOElement2 The value of the value attribute
VO.VOElement3 = (row.SelectSingleNode("Cell[@Name='VOElement3']") as XmlElement).GetAttribute("Value").ToString();
voList.Add( VO);
}
return voList;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn