第一部分
----基本信息
1、 如何顯示xml中的信息,運用xsl:具體如何實現如下:
protected System.Web.UI.WebControls.Xml myXml;----聲明一個xml對象
//加載xml信息
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.Load(Server.MapPath("book.xml"));
this.myXml.Document=xmlDoc;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//加載樣式表
XslTransform xslTrans=new XslTransform();
xslTrans.Load(Server.MapPath("book.xsl"));
myXml.Transform=xslTrans;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、 如果在client端加載讀取信息,操作如下:
1、 通過一定的條件來讀取:
var dom =new ActiveXObject"msxml2.DOMDocument.3.0");
var re;
re = /\s/g;
dom.async = false;
dom.validateOnParse = false;
dom.resolveExternals = false;
var xpath,show_message;
dom.load("xml/pensionlist.xml");
xpath="/Persions/persion[@Staff_id='"+ per_id +"']";
node1=dom.selectSingleNode(xpath);
if (node1 != null){
node1.childNodes(0).text----第一個節點的信息
node1.childNodes(9).childNodes(0).text-----第十個節點的第一個子節點信息
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、 無條件的讀取:
var oDoc = new ActiveXObject"Microsoft.XMLDOM");
oDoc.load("1.xml");
var objFile1=oDoc.selectSingleNode("//New_CODE");---直接取得的
strcno=objFile1.text;
var root=oDoc.documentElement;
var objFile=root.selectNodes("//SUB_SEQ");-----可以進行循環的
for(var inti=0;inti<objFile.length;inti++)
{
alert(objFile.item(inti).text);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3、 新增信息到xml文檔中,操作如下:(server端操作)
第一種方法:
XmlDocument xmlDoc=new XmlDocument();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.Load(Server.MapPath("book.xml"));-------加載信息
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
if(xmlDoc.DocumentElement.IsEmpty)
{------------判斷是否為空
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmlDoc.LoadXml("<gusetbook/>");----------沒有的話新增
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlNode xmlNote=xmlDoc.DocumentElement.AppendChild(xmlDoc.CreateElement("book"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----新增第一個節點:book,下面是加其節點的兩個屬性:時間:date及代號:Staff_id
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlAttribute xmlattr=xmlDoc.CreateAttribute("date");---創建一個屬性date
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlattr.Value=System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");--設定屬性信息
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlNote.Attributes.Append(xmlattr);----在book節點上新增一個屬性
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//新增另一個屬性,要找出本xml文檔中的最大號,進行比較
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlElement xmld = xmlDoc.DocumentElement;----聲明一元素
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string strmax="";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int count_elem=0;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
count_elem=xmld.ChildNodes.Count - 2;-----找到最大節點的index
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlElement nodeee = (XmlElement)xmld.ChildNodes[count_elem];──找到此節點並轉類型
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
strmax=nodeee.GetAttribute("Staff_id");---取到想要的信息
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlattr=xmlDoc.CreateAttribute("Staff_id");-----增加些元素
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlattr.Value=strmax.Substring(0,5)+(int.Parse(strmax.Substring(5,1))+1) ;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlNote.Attributes.Append(xmlattr);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//增加一個型式的節點
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
childnote=xmlNote.AppendChild(xmlDoc.CreateElement("adress"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
childnote.InnerText=stradress;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//add skill----增加另一個樣式的節點,此節點又有子節點
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
childnote=xmlNote.AppendChild(xmlDoc.CreateElement("P_Skills"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
for(int i=0;i<3;i++)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
XmlNode childsnote=null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
childsnote=childnote.AppendChild(xmlDoc.CreateElement("P_Skill" & (i+1)));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
childsnote.InnerText="java"+i;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//增加有data樣式的節點,如<message><![CDATA[xxxxxxxx]]> </message>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
childnote=xmlNote.AppendChild(xmlDoc.CreateElement("message"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
childnote.AppendChild(xmlDoc.CreateCDataSection(strmessage));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//保存新增後的文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.Save(Server.MapPath("book.xml"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第二種方法:
XmlDocument xmlDoc=new XmlDocument();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.Load(Server.MapPath("users.xml"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlNode xmlRoot=xmlDoc.DocumentElement;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlElement xmlUser=xmlDoc.CreateElement("User");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlElement xmlName=xmlDoc.CreateElement("Name");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlElement xmlEmail=xmlDoc.CreateElement("Email");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlElement xmlPassword=xmlDoc.CreateElement("Password");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlName.InnerText="addName";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlEmail.InnerText="addEmail";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlPassword.InnerText="addPassword";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlUser.AppendChild(xmlName);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlUser.AppendChild(xmlEmail);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlUser.AppendChild(xmlPassword);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlAttribute xmlattr=xmlDoc.CreateAttribute("newAttr");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlattr.Value="AttributeValue";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.DocumentElement.SetAttributeNode(xmlattr);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlRoot.AppendChild(xmlUser);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4、 修改一畢資料,在server端,操作如下:
XmlDocument xmlDoc= new XmlDocument();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.Load(Server.MapPath("book.xml"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string xpth_str="";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xpth_str="/guestbook/book[@Staff_id='" + this.txtCNO_ID.Text + "']";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
System.Xml.XmlElement xmlet=xmlDoc.DocumentElement;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
System.Xml.XmlNode xmnode=xmlet.SelectSingleNode(xpth_str);-------取得有條件相關節點
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmnode.ChildNodes[4].ChildNodes[0].InnerText=xmnode.ChildNodes[4].ChildNodes[0].InnerText.ToString()+"66";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmnode.ChildNodes[4].ChildNodes[1].InnerText=xmnode.ChildNodes[4].ChildNodes[1].InnerText.ToString()+"66";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmnode.ChildNodes[4].ChildNodes[2].InnerText=xmnode.ChildNodes[4].ChildNodes[2].InnerText.ToString()+"66";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////以上為讀取有子節點的節點相關資料並進修改![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string strmess=xmnode.ChildNodes[5].InnerText.ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
System.Xml.XmlNode mess=xmnode.SelectSingleNode("message");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
mess.RemoveAll();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
mess.AppendChild(xmlDoc.CreateCDataSection(strmess+"_upd"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//修改data資料
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.Save(Server.MapPath("book.xml"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5、 刪除指定節點,操作如下:(在server端)
XmlDocument xmlDoc= new XmlDocument();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.Load(Server.MapPath("book.xml"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string xpth_str="";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xpth_str="/guestbook/book[@Staff_id='" + this.txtCNO_ID.Text + "']";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
System.Xml.XmlElement xmlet=xmlDoc.DocumentElement;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlet.RemoveChild(xmlet.SelectSingleNode(xpth_str));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
xmlDoc.Save(Server.MapPath("book.xml"));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第二部分
───xsl的語法運用
1、 xml的語法結構:
<?xml version="1.0" encoding="utf-8"?>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如下是一個本測試中的example:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<guestbook>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<book date="2006-01-05 18:10:00" Staff_id="c03031">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<name>Susan</name>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<email>snowonyou@eyou.com</email>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<homepage>http://www.hao123.com</homepage>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<adress>河南許昌</adress>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<P_Skills>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<P_Skill1>ASP</P_Skill1>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<P_Skill2>ASP.NET</P_Skill2>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<P_Skill3>SQL</P_Skill3>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</P_Skills>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<message><![CDATA[這是我第2個用xml做的例子,學習學習,加油!!不認輸!_upd]]></message>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</book>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</guestbook>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、 xsl的語法結權
<?xml version="1.0" encoding="utf-8" ?>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:template match="guestbook">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
,,,,,,,對相關資料進行操作
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:template>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:stylesheet>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下面是一個例子:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:template match="guestbook">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<body bgcolor="#CCCC66" onselectstart="return false">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<center><h1>留言版</h1></center>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<table border="1" width="100%" bgcolor="#bbCCCC">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr bgcolor="#0099CC">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td>最新留言||<a href="../index.html">我要留言</a></td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<table width="100%" border="1">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:for-each select="book">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:sort select="@date"></xsl:sort>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr bgcolor="#bbCC00">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td rowspan="2" width="20%">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
學號:<xsl:value-of select="substring(@Staff_id, 1, 3)"></xsl:value-of>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
留言人:<xsl:value-of select="name"></xsl:value-of><p/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
來自:<xsl:value-of select="adress"></xsl:value-of><p/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
技能:<xsl:value-of select="P_Skills/P_Skill1" />、
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:value-of select="P_Skills/P_Skill2" />、
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:value-of select="P_Skills/P_Skill3" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td width="80%">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
留言時間:<xsl:value-of select="@date"></xsl:value-of>||
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Email:<xsl:value-of select="email"></xsl:value-of>||
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
個人主頁:<xsl:value-of select="homepage"></xsl:value-of>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr >
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:attribute name="ONCLICK">alert("呵呵,原來是這樣呀");</xsl:attribute>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:attribute name="oncontextmenu">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
window.event.cancelBubble _u32 ?true;check_log();loadContextMenu('context.xml',
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
'<xsl:value-of select="@Staff_id"></xsl:value-of>',
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
'<xsl:value-of select="name"></xsl:value-of>');return false;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:attribute>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td width="72%">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:choose>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:when test="position()=last()">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<a><xsl:attribute name="title">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
這是最後一畢
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:attribute>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<pre><xsl:value-of select="message"></xsl:value-of></pre>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</a>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:when>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<xsl:otherwise>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<pre><xsl:value-of select="message"></xsl:value-of></pre>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:otherwise>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:choose>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:for-each>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</table>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</table>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:template>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</xsl:stylesheet>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)