后台XML操作
下面分别记录了2种对XML文档的操作类以及方法和属性。其实这2种操作原理都是一样,区别在于一个是.net原由的XmlDocument类和与 其对应的相关类、方法属性等,另一个则是最新的.net3.5中才有的XDocument类和与其对应的相关类、方法属性等。
需要加的二个命名空间是
using System.IO;
using System.Web;
公共部分代码很少,如下:
接着就是具体对XML文档操作的代码,如下:
(1)、 XDocument类、XElement 类、XAttribute 类
上面3个类的必须添加System.Xml.Linq命名空间才能使用。
下面是把更、删、改写在一个方法里,如果需要可以单独针对每个操作写对应的方法。
以上的就是用Linq来对XML操作的代码。
(2)、XmlDocument类、XmlElement类、XmlAttribute类
上面的3个类需要引用System.Xml命名空间。
(这块代码晚上回去在贴上来)
前台操作。
前台的显示有好多办法,而我则用JQUERY中的AJAX来显示。这样显示比较麻烦,因为在实际显示中,碰到了兼容问题。IE6和Firefox对 XML文挡的节点找寻不能统一,只能分别显示。代码有点多,以后可能会发现更精简的显示办法。
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default.aspx.cs " Inherits = " _Default " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 无标题页 </ title >
< script src ="jquery.js" type ="text/javascript" ></ script >
< script type ="text/javascript" >
// 排序用的对象
var XmlNodeList = function (lwid,date,orderby,name,age,sex,city,hometown){
this .lwid = lwid;
this .date = date;
this .orderby = orderby;
this .name = name;
this .age = age;
this .sex = sex;
this .city = city;
this .hometown = hometown;
};
// 把内容存放在数组里再排序
var arrlist = new Array();
// 读数据
function ShowXml()
{
var aid = "" ;
var date = "" ;
var orderby = "" ;
var name = "" ;
var age = "" ;
var sex = "" ;
var city = "" ;
var hometown = "" ;
$.ajax({
type: " GET " ,
url: " liuwei.xml " ,
dataType: " xml " ,
data: "" ,
cache: false ,
success: function (data)
{
var html = "" ;
var len = data.documentElement.childNodes.length;
// 这里的IF[判断XML文档是否有数 据
if (len == 0 || (len == 1 &&
data.documentElement.childNodes[ 0 ].nodeName == " #text " ))
{
html += " <ul><li> 无任何内容</li></ul> " ;
}
else
{
for ( var i = 0 ;i < data.documentElement.childNodes.length;i ++ )
{
// 因为火狐在分析XML文档时总把子结 点与子结点之间添加了一个
// "/n "结点,不知道这是为什 么,所以必须要下面的判断
if (data.documentElement.childNodes[i].childNodes.length > 0 )
{
// 这里的index1--index5 的值是针对IE6时结点的索引。
// 每个 Root根结点下面的Xml_Linq子结点下面的子结点只有5个。
var index1 = 0 ;
var index2 = 1 ;
var index3 = 2 ;
var index4 = 3 ;
var index5 = 4 ;
if (isFirefox = navigator.userAgent.indexOf( " Firefox " ) > 0 )
{
// 上面说过为什么要区分 FireFox,所以这里
// 就必须 是相隔一位的索引,不然就会报错
index1 = 1 ;
index2 = 3 ;
index3 = 5 ;
index4 = 7 ;
index5 = 9 ;
}
aid = data.documentElement.childNodes[i].attributes.getNamedItem( " id " ).nodeValue;
date = data.documentElement.childNodes[i].attributes.getNamedItem( " date " ).nodeValue;
orderby = parseInt(data.documentElement.childNodes[i].attributes.getNamedItem( " orderby " ).nodeValue);
name = data.documentElement.childNodes[i].childNodes[index1].firstChild.nodeValue;
age = data.documentElement.childNodes[i].childNodes[index2].firstChild.nodeValue;
sex = data.documentElement.childNodes[i].childNodes[index3].firstChild.nodeValue;
city = data.documentElement.childNodes[i].childNodes[index4].firstChild.nodeValue;
hometown = data.documentElement.childNodes[i].childNodes[index5].firstChild.nodeValue;
// 把数据放在数组里,这样下面就可以对 这些数据排序
arrlist[i] = new XmlNodeList(aid,date,orderby,name,age,sex,city,hometown);
}
}
// 排序内容
arrlist.sort( function (tObj,sObj){ return sObj.orderby - tObj.orderby});
for ( var j = 0 ;j < arrlist.length;j ++ )
{
// 这里是对火狐的区分,如果不加火狐就 会出现错误
if (arrlist[j] != undefined)
{
var id = arrlist[j].lwid.toString();
html += " <ul id='lw_ " + id + " '><li>id:<span id='id_ " + id + " '> " ;
html += " <a href='javascript:void(0)' onclick=Update(' " + id + " ')> " + id + " </a></span></li> " ;
html += " <li>date:<span id='date_ " + id + " '> " + arrlist[j].date.toString() + " </span></li> " ;
html += " <li>name:<span id='name_ " + id + " '> " + arrlist[j].name.toString() + " </span></li> " ;
html += " <li>age:<span id='age_ " + id + " '> " + arrlist[j].age.toString() + " </span></li> " ;
html += " <li>sex:<span id='sex_ " + id + " '> " + arrlist[j].sex.toString() + " </span></li> " ;
html += " <li>city:<span id='city_ " + id + " '> " + arrlist[j].city.toString() + " </span></li> " ;
html += " <li>hometown:<span id='hometown_ " + id + " '> " + arrlist[j].hometown.toString() + " </span></li> " ;
html += " <li>orderby:<span id='orderby_ " + id + " '> " + arrlist[j].orderby.toString() + " </span></li> " ;
html += " <li><input type='checkbox' id='cb_ " + id + " ' name='cb' onclick=Del(this);></li></ul> " ;
}
}
}
$( " #show_xml " ).html(html);
},
error: function (a,b,c){
alert(c);
}
});
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< div id ="showresult" ></ div >
< div >
< asp:Button ID ="btnCreateXml" runat ="server" Text ="创建XML" OnClick ="btnCreateXml_Click" />
</ div >
< input type ="hidden" name ="option" id ="option" value ="" />
< input type ="hidden" id ="id" name ="id" value ="" />
< div id ="Insert" >
<!--
这里与上面一样 -->
< p > name: < input type ="text" name ="name" id ="name" value ="" /></ p >
< p > age: < input type ="text" name ="age" id ="age" value ="" /></ p >
< p > sex: < input type ="text" name ="sex" id ="sex" value ="" /></ p >
< p > city: < input type ="text" name ="city" id ="city" value ="" /></ p >
< p > hometown: < input type ="text" name ="hometown" id ="hometown" value ="" /></ p >
< p > orderby: < input type ="text" name ="orderby" id ="orderby" value ="" /></ p >
< div >< asp:Button ID ="btnCreateNode" runat ="server" Text ="创建 XmlNode" OnClick ="btnCreateNode_Click" /></ div >
</ div >
< div id ="Update" style ="display:none;" >
< p > name: < input type ="text" name ="name_U" id ="name_U" value ="" /></ p >
< p > age: < input type ="text" name ="age_U" id ="age_U" value ="" /></ p >
< p > sex: < input type ="text" name ="sex_U" id ="sex_U" value ="" /></ p >
< p > city: < input type ="text" name ="city_U" id ="city_U" value ="" /></ p >
< p > hometown: < input type ="text" name ="hometown_U" id ="hometown_U" value ="" /></ p >
< p > orderby: < input type ="text" name ="orderby_U" id ="orderby_U" value ="" /></ p >
< div >< asp:Button ID ="btnUpdate" runat ="server" Text ="更新" OnClick ="btnUpdate_Click" /></ div >
</ div >
< div id ="show_xml" ></ div >
< input type ="hidden" name ="ids" id ="ids" value ="" />
< asp:Button ID ="btnDel" runat ="server" Text ="删除所选项" OnClick ="btnDel_Click" />
</ form >
< script type ="text/javascript" >
ShowXml();
function Update(id)
{
$( " #id " ).val(id);
$( " #date_U " ).val($( " #date_ " + id).html());
$( " #name_U " ).val($( " #name_ " + id).html());
$( " #age_U " ).val($( " #age_ " + id).html());
$( " #sex_U " ).val($( " #sex_ " + id).html());
$( " #city_U " ).val($( " #city_ " + id).html());
$( " #hometown_U " ).val($( " #hometown_ " + id).html());
$( " #orderby_U " ).val($( " #orderby_ " + id).html());
$( " #Insert " ).hide();
$( " #Update " ).show();
location.href = " #Update " ;
}
var ids = "" ;
function Del(obj)
{
if (obj.checked == true )
{
ids += obj.id.replace( " cb_ " , "" ) + " , " ;
}
else
{
ids = ids.replace(obj.id.replace( " cb_ " , "" ) + " , " , "" );
}
$( " #ids " ).val(ids);
}
</ script >
</ body >
</ html >