XML+XSL+C#=>html

想了解DataSet在XML中的相关操作,先了解了XML文件+XSL文件+C#=>html:

1.xml: s.xml <?xml...?>

2.xsl: s.xsl

<xsl:stylesheet...>

<xsl:output method="html"/>

<xsl:template match="/">

<html>....xpath ....</html>

(如果是显示服务器端的数据库数据:

<xsl:for-each select="//表名">

<tr>

<th><xsl:value-of select="字段名"/></th>

</tr>

</xsl:for-each>

还可以在这个的基础上添加样式)

</xsl:template>

</xsl:stylesheet>

3.c#转换

using System;xml;xml.xsl;xml.xpath; using System.IO;using System.Text;

namespace xmlsamp

{class Program

{

[STAThread]

static void Main(string[] args)

{

XmlReader read = XmlReader.Create("myxml。xml");//所有文件默认在BIN-Debug下

//此处,如果没有XML文件,可以用XML字符串来创建,也可以的

TextReader tr = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b>h</b><b>c</b></a>");

XmlReader read = XmlReader.Create(tr);

XPathDocument xpathdocument = new XPathDocument(read);

XmlTextWriter xmltextwrite = new XmlTextWriter("myhtml。html", null);

XslCompiledTransform xsltransform =new XslCompiledTransform();

xsltransform.Load("http://myxsl。xsl");

xsltransform.Transform(xpathdocument, null, xmltextwrite);

xmltextwrite.close();

}

}

}

总结:XML文件+XSL文件+C#=>html:

也就是:XPathDocument.xml(xml源)+XslCompiledTransform.Load.xsl(xsl样式)+xsltransform.Transform(xml源, null, XmlTextWriter)=>XmlTextWriter.html

另一种,在服务器端连接了数据库,C# 代码如下:

using (SqlConnection con = new SqlConnection("Server=.;DataBase=HGSTUDY;u))

{

con.Open();

SqlCommand command = new SqlCommand("select * from GL_STUDY for xml auto,elements",con);

XmlReader reader = command.ExecuteXmlReader();

XPathDocument xpath = new XPathDocument(reader);

XslCompiledTransform xslcom = new XslCompiledTransform();

xslcom.Load("http://myxsl。xsl");

xslcom.Transform(xpath,null,Console.Out);

//如果不是控制台应用程序:xslcom.Transform(xpath,null,Response.Output);

reader.Close();记得关闭

xtw.Close();记得关闭,否则打不开这个文件的

}

总结:XPathDocument的数据源要是一个XmlReader实例,不同的是:XmlReader实例的创建方式不同

1。数据库的数据源是SqlCommand的for xml auto,elements生成的。然后XslCompiledTransform加载(.Load),转换(.Transform)。

2。XML文档数据,则要初始化一个XmlReader实例,通过他的Create方法:XmlReader read = XmlReader.Create("myxml。xml");

XPathDocument xpathdocument = new XPathDocument(read);

这两句相当于一句:XPathDocument xpathdocument = new XPathDocument("myxml。xml");

3。还可以是URL,流,字符串和其他的XML输入

注意归纳如下:

数据源(XPathDocument的实例)的形式是多种,目前我发现1。可以是XML文件,2。可以是command执行的一个SQL查询语句,但是该SQL语句一定要有for xml..呵呵

转换的形式(Transform最后的那个参数)也可以是多种形式,目前我发现1。可以是Console.Out,适合在控制台应用程序中,2。可以是Response.Output,适合在网页中,显示在自己的页面中,3。可以是一个生成XML数据的流或文件的编写器XmlTextWriter,这个用于生成一个实实在在的HTML文件。不过如果名字相同,是会覆盖原文件,用于自动生成HTML文件。

补充:20130107

感谢网友荣浩!

除我之前的两种,XPathDocument 加载还有网友的第三种加载方式:

1:XmlReader read = XmlReader.Create("myxml。xml");

  XPathDocument xpathdocument = new XPathDocument(read);

2:SqlCommand command = new SqlCommand("select * from GL_STUDY for xml auto,elements",con);

  XmlReader reader = command.ExecuteXmlReader();

  XPathDocument xpath = new XPathDocument(reader);

3:不同于第2种方式,将结果利用DATASET可以转换为XML的方法,加载到XPathDocument

  SqlCommand command = new SqlCommand("select * from GL_STUDY ",con);

  DataSet ds = new DataSet();

  SqlDataAdapter da = new SqlDataAdapter(command);

  da.Fill(ds);

  byte[] b = Encoding.ASCII.GetBytes(ds.GetXml());

  MemoryStream stream = new MemoryStream(b);

  XPathDocument xpath = new XPathDocument(stream);

欢迎大家的评论哈