ASP.NET和C#中对XML的操作,以及简单的xml与xsl !

随着不断的学习,对asp.net c#的了解,越来越觉得xml很好,但是有些还是要和sql联用,并不象有些人夸大说的替代sql了,这里讲讲我自己的用的一些经验。

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种常用的方法

1、往<bookstore>节点中插入一个<book>节点:

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 version="1.0"?>

<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<NewDataSet>

<Table >

<ProductID>1001</ProductID>

<CategoryID>1</CategoryID>

</Table>

<Table >

<ProductID>1002</ProductID>

<CategoryID>2</CategoryID>

</Table>

</NewDataSet>

其中第2句是引用xsl,xsl内容如下

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">

<html>

<body>

<center>

<h2>the notepad</h2>

<table >

<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[@]/ProductID"/>

<?xml version="1.0" encoding="gb2312"?>

<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[@]/ProductID"/></h1></center>

</xsl:template>

</xsl:stylesheet>

有些如果在Table下还有节点,要显示这个节点下的东西xsl就该如下

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">

<html>

<body>

<center>

<h2>the notepad</h2>

<table >

<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>

差不多了,收工,呵呵,大家交流哈!