ASP.NET和C#中对XML的操作,以及简单的xml与xsl !
?????? asp.net創建xml就是通過創建DataTable來創建xml中的樹型等
?1?DataSet?objset=new?DataSet();
?2?????????DataTable?istable=new?DataTable("test");
?3?????????istable.Columns.Add("rate1",typeof(int));
?4?????????????????istable.Columns.Add("rate2",typeof(int));
?5?????????????????istable.Columns.Add("rate3",typeof(int));
?6?????????????????istable.Columns.Add("rate4",typeof(int));
?7?????????objset.Tables.Add(istable);
?8?????????
?9?????????DataRow?dr=objset.Tables["test"].NewRow();
10?????????dr[0]=賦值;
11?????????dr[1]=賦值;
12?????????dr[2]=賦值;
13?????????dr[3]=賦值;
14?????????objset.Tables["money"].Rows.Add(dr);
15?????????
16?????????objset.WriteXml(Server.MapPath("test.xml"),XmlWriteMode.WriteSchema);
??????? 其中就是先創建DataSet和DataTable,其中建立的表為test,再在表中添加子項rate1,2,3,4,再定義新的行,分別添加對應的值,最后這些都已經寫進DataSet表中,通過DataSet把xml輸入就完成了。要讀就只需要把xml的數據讀到DataSet中,再通過DataSet中的表的項來對應讀出數據。
??????? 而C#中則使用的DOM來實現操作,比如現有一個bookstore.xml文件,內容如下
??????? <?xml version="1.0" encoding="gb2312"?>
????????<bookstore>
??????????<book genre="fantasy" ISBN="2-3631-4">
???????????? <title>Oberon's Legacy</title>
???????????? <author>Corets, Eva</author>
???????????? <price>5.95</price>
??????????</book>
??????? </bookstore>
??????? 下面講解4種常用的方法
???XmlDocument?xmlDoc=new?XmlDocument();
???xmlDoc.Load("bookstore.xml");
???XmlNode?root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
???XmlElement?xe1=xmlDoc.CreateElement("book");//創建一個<book>節點
???xe1.SetAttribute("genre","李贊紅");//設置該節點genre屬性
???xe1.SetAttribute("ISBN","2-3631-4");//設置該節點ISBN屬性
?
???XmlElement?xesub1=xmlDoc.CreateElement("title");
???xesub1.InnerText="CS從入門到精通";//設置文本節點
???xe1.AppendChild(xesub1);//添加到<book>節點中
???XmlElement?xesub2=xmlDoc.CreateElement("author");
???xesub2.InnerText="候捷";
???xe1.AppendChild(xesub2);
???XmlElement?xesub3=xmlDoc.CreateElement("price");
???xesub3.InnerText="58.3";
???xe1.AppendChild(xesub3);
???
???root.AppendChild(xe1);//添加到<bookstore>節點中
???xmlDoc.Save("bookstore.xml");
???//================
???結果為:
???
???<?xml?version="1.0"?encoding="gb2312"?>
???<bookstore>
????<book?genre="fantasy"?ISBN="2-3631-4">
?????<title>Oberon's?Legacy</title>
?????<author>Corets,?Eva</author>
?????<price>5.95</price>
????</book>
????<book?genre="李贊紅"?ISBN="2-3631-4">
?????<title>CS從入門到精通</title>
?????<author>候捷</author>
?????<price>58.3</price>
????</book>
???</bookstore>
??2、修改節點:將genre屬性值為“李贊紅“的節點的genre值改為“update李贊紅”,將該節點的子節點<author>的文本修改為“亞勝”。
???XmlNodeList?nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//獲取bookstore節點的所有子節點
???foreach(XmlNode?xn?in?nodeList)//遍歷所有子節點
???{
???????XmlElement?xe=(XmlElement)xn;//將子節點類型轉換為XmlElement類型
???????if(xe.GetAttribute("genre")=="李贊紅")//如果genre屬性值為“李贊紅”
???????{
???????????xe.SetAttribute("genre","update李贊紅");//則修改該屬性為“update李贊紅”
???????????XmlNodeList?nls=xe.ChildNodes;//繼續獲取xe子節點的所有子節點
???????????foreach(XmlNode?xn1?in?nls)//遍歷
???????????{
???????????????XmlElement?xe2=(XmlElement)xn1;//轉換類型
???????????????if(xe2.Name=="author")//如果找到
???????????????{
???????????????????xe2.InnerText="亞勝";//則修改
???????????????????break;//找到退出來就可以了
???????????????}
???????????}
???????????break;
???????}
???}
?
???xmlDoc.Save("bookstore.xml");//保存。
???//=================
???最后結果為:
???<?xml?version="1.0"?encoding="gb2312"?>
???<bookstore>
????<book?genre="fantasy"?ISBN="2-3631-4">
?????<title>Oberon's?Legacy</title>
?????<author>Corets,?Eva</author>
?????<price>5.95</price>
????</book>
????<book?genre="update李贊紅"?ISBN="2-3631-4">
?????<title>CS從入門到精通</title>
?????<author>亞勝</author>
?????<price>58.3</price>
????</book>
???</bookstore>
??3、刪除?<book?genre="fantasy"?ISBN="2-3631-4">節點的genre屬性,刪除?<book?genre="update李贊紅"?ISBN="2-3631-4">節點。
???XmlNodeList?xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
?
???foreach(XmlNode?xn?in?xnl)
???{
??????XmlElement?xe=(XmlElement)xn;
?
??????if(xe.GetAttribute("genre")=="fantasy")
??????{
??????????xe.RemoveAttribute("genre");//刪除genre屬性
??????}
??????else?if(xe.GetAttribute("genre")=="update李贊紅")
??????{
??????????xe.RemoveAll();//刪除該節點的全部內容
??????}
???}
???xmlDoc.Save("bookstore.xml");
???//====================
??
???最后結果為:
???<?xml?version="1.0"?encoding="gb2312"?>
???<bookstore>
????<book?ISBN="2-3631-4">
?????<title>Oberon's?Legacy</title>
?????<author>Corets,?Eva</author>
?????<price>5.95</price>
????</book>
????<book>
????</book>
???</bookstore>?
??4、顯示所有數據。
???XmlNode?xn=xmlDoc.SelectSingleNode("bookstore");?
???XmlNodeList?xnl=xn.ChildNodes;
???
???foreach(XmlNode?xnf?in?xnl)
???{
???????XmlElement?xe=(XmlElement)xnf;
???????Console.WriteLine(xe.GetAttribute("genre"));//顯示屬性值
???????Console.WriteLine(xe.GetAttribute("ISBN"));
?
???????XmlNodeList?xnf1=xe.ChildNodes;
???????foreach(XmlNode?xn2?in?xnf1)
???????{
???????????Console.WriteLine(xn2.InnerText);//顯示子節點點文本
???????}
???}??
??????? 以上的就是ASP.NET和C#對xml的基本使用方法。下面說一下xml和xsl,從抽象的來說,我個人覺得xml象是數據庫,而xsl就象是過濾的。xsl中可以加入html等語法,也可以加入xml的語法等,可以列出需要的數據等。
現有一個xml文件,內容如下
<?xml-stylesheet?type="text/xsl"?href="test.xsl"?>
<NewDataSet>
?<Table?id="1">
????<ProductID>1001</ProductID>
????<CategoryID>1</CategoryID>
?</Table>
?<Table?id="2">
????<ProductID>1002</ProductID>
????<CategoryID>2</CategoryID>
?</Table>
</NewDataSet>
其中第2句是引用xsl,xsl內容如下
<xsl:stylesheet?xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template?match="/">
<html>
?????<body>
???????<center>
???????<h2>the?notepad</h2>
???????<table?border="1">
???????????<tr>
????????????????<td>id</td>
????????????????<td>name</td>
???????????</tr>
??????<xsl:for-each?select="NewDataSet/Table">
???????????<tr>
????????????????<td><xsl:value-of?select="ProductID"/></td>
????????????????<td><xsl:value-of?select="ProductName"/></td>
????????????</tr>
??????</xsl:for-each>
???????</table>
???????</center>
?????</body>
????</html>
?</xsl:template>
</xsl:stylesheet>
中間的for-each就是循環遍歷節點,獲取指定的select后的內容,下面的value-of就相當于sql中字段名。在使用xsl的時候,還可以查詢某一個值,這樣xsl就需要如下<xsl:value-of?select="/students/student[@id='2']/ProductID"/>
<xsl:stylesheet?xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template?match="/">
<center>?<h1>id號是"2"的廠家的產品ID是:<xsl:value-of?select="/NewDataSet/Table[@id='2']/ProductID"/></h1></center>
?</xsl:template>
</xsl:stylesheet>
有些如果在Table下還有節點,要顯示這個節點下的東西xsl就該如下
<xsl:stylesheet?xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template?match="/">
????<html>
?????<body>
???????<center>
???????<h2>the?notepad</h2>
???????<table?border="1">
???????????<tr>
????????????????<td>隨便寫</td>
???????????</tr>
??????<xsl:for-each?select="NewDataSet/Table/xx/*">
???????????<tr>
????????????????<td><xsl:value-of?select="."/></td>
????????????</tr>
??????</xsl:for-each>
???????</table>
???????</center>
?????</body>
????</html>
</xsl:template>
</xsl:stylesheet>
??????? 差不多了,收工,呵呵,大家交流哈!
轉載于:https://www.cnblogs.com/xujiaci/archive/2007/09/01/878193.html
總結
以上是生活随笔為你收集整理的ASP.NET和C#中对XML的操作,以及简单的xml与xsl !的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不必要的损失
- 下一篇: 获取和设置c# 应用程序目录