ASP.NET和C#中对XML的操作,以及简单的xml与xsl !
?????? asp.net創(chuàng)建xml就是通過創(chuàng)建DataTable來創(chuàng)建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);
??????? 其中就是先創(chuàng)建DataSet和DataTable,其中建立的表為test,再在表中添加子項(xiàng)rate1,2,3,4,再定義新的行,分別添加對應(yīng)的值,最后這些都已經(jīng)寫進(jìn)DataSet表中,通過DataSet把xml輸入就完成了。要讀就只需要把xml的數(shù)據(jù)讀到DataSet中,再通過DataSet中的表的項(xiàng)來對應(yīng)讀出數(shù)據(jù)。
??????? 而C#中則使用的DOM來實(shí)現(xiàn)操作,比如現(xiàn)有一個bookstore.xml文件,內(nèi)容如下
??????? <?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");//創(chuàng)建一個<book>節(jié)點(diǎn)
???xe1.SetAttribute("genre","李贊紅");//設(shè)置該節(jié)點(diǎn)genre屬性
???xe1.SetAttribute("ISBN","2-3631-4");//設(shè)置該節(jié)點(diǎn)ISBN屬性
?
???XmlElement?xesub1=xmlDoc.CreateElement("title");
???xesub1.InnerText="CS從入門到精通";//設(shè)置文本節(jié)點(diǎn)
???xe1.AppendChild(xesub1);//添加到<book>節(jié)點(diǎn)中
???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>節(jié)點(diǎn)中
???xmlDoc.Save("bookstore.xml");
???//================
???結(jié)果為:
???
???<?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、修改節(jié)點(diǎn):將genre屬性值為“李贊紅“的節(jié)點(diǎn)的genre值改為“update李贊紅”,將該節(jié)點(diǎn)的子節(jié)點(diǎn)<author>的文本修改為“亞勝”。
???XmlNodeList?nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//獲取bookstore節(jié)點(diǎn)的所有子節(jié)點(diǎn)
???foreach(XmlNode?xn?in?nodeList)//遍歷所有子節(jié)點(diǎn)
???{
???????XmlElement?xe=(XmlElement)xn;//將子節(jié)點(diǎn)類型轉(zhuǎn)換為XmlElement類型
???????if(xe.GetAttribute("genre")=="李贊紅")//如果genre屬性值為“李贊紅”
???????{
???????????xe.SetAttribute("genre","update李贊紅");//則修改該屬性為“update李贊紅”
???????????XmlNodeList?nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點(diǎn)的所有子節(jié)點(diǎn)
???????????foreach(XmlNode?xn1?in?nls)//遍歷
???????????{
???????????????XmlElement?xe2=(XmlElement)xn1;//轉(zhuǎn)換類型
???????????????if(xe2.Name=="author")//如果找到
???????????????{
???????????????????xe2.InnerText="亞勝";//則修改
???????????????????break;//找到退出來就可以了
???????????????}
???????????}
???????????break;
???????}
???}
?
???xmlDoc.Save("bookstore.xml");//保存。
???//=================
???最后結(jié)果為:
???<?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">節(jié)點(diǎn)的genre屬性,刪除?<book?genre="update李贊紅"?ISBN="2-3631-4">節(jié)點(diǎn)。
???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();//刪除該節(jié)點(diǎn)的全部內(nèi)容
??????}
???}
???xmlDoc.Save("bookstore.xml");
???//====================
??
???最后結(jié)果為:
???<?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、顯示所有數(shù)據(jù)。
???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);//顯示子節(jié)點(diǎn)點(diǎn)文本
???????}
???}??
??????? 以上的就是ASP.NET和C#對xml的基本使用方法。下面說一下xml和xsl,從抽象的來說,我個人覺得xml象是數(shù)據(jù)庫,而xsl就象是過濾的。xsl中可以加入html等語法,也可以加入xml的語法等,可以列出需要的數(shù)據(jù)等。
現(xiàn)有一個xml文件,內(nèi)容如下
<?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內(nèi)容如下
<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就是循環(huán)遍歷節(jié)點(diǎn),獲取指定的select后的內(nèi)容,下面的value-of就相當(dāng)于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"的廠家的產(chǎn)品ID是:<xsl:value-of?select="/NewDataSet/Table[@id='2']/ProductID"/></h1></center>
?</xsl:template>
</xsl:stylesheet>
有些如果在Table下還有節(jié)點(diǎn),要顯示這個節(jié)點(diǎn)下的東西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>
??????? 差不多了,收工,呵呵,大家交流哈!
轉(zhuǎn)載于:https://www.cnblogs.com/xujiaci/archive/2007/09/01/878193.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET和C#中对XML的操作,以及简单的xml与xsl !的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不必要的损失
- 下一篇: Excel 2007中不可不知的数字