C#利用Xml Transform把xml和Xlst转换为html

http://msdn.microsoft.com/zh-tw/library/ms163439%28v=vs.100%29.aspx
http://www.xmlfiles.com/articles/sample_chapters/sams_xmlforaspnet/
http://mynerdjournal.wordpress.com/2010/05/18/xml-to-html-with-xslt-in-asp-net-mvc/
http://tpeczek.blogspot.com/2009/12/xml-and-xslt-transformation-in-aspnet_21.html
http://www.xmlfiles.com/articles/sample_chapters/sams_xmlforaspnet/
http://blog.janjonas.net/2012-09-07/asp_net-mvc_4_rc-web-api-implementing-custom-xmlmediatypeformatter-ignore-xml-namespace
http://tutorialgenius.blogspot.com/2011/06/c-mvc-3-render-xslt-htmlhelper.html
http://stackoverflow.com/questions/3453518/need-help-preforming-an-xsl-transform-from-xml-to-html-in-asp-net-mvc
http://blog.dezfowler.com/2008/05/creating-views-using-xsl-in-aspnet-mvc.html
http://stackoverflow.com/questions/13142469/transforming-xml-as-a-string-with-xslt-as-a-string-in-c-sharp-net-4-0-invalid

http://www.codeproject.com/Articles/37868/Beginners-Introduction-To-XSL-Transform-Rendering


XML
<BookStore>
     <Book>
         <Id>1</Id>
         <Name>SQL Server 2005宝典</Name>
         <Author>Paul Nielsen</Author>
         <Publisher>人民邮电出版社</Publisher>
         <PubDate>2006-10-01T00:00:00+08:00</PubDate>
         <Price>65.50</Price>
     </Book>

 </BookStore>


XLST文件


<?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet version="1.0"
                      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    
                      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                      exclude-result-prefixes="msxsl">
     <xsl:output method="html" indent="yes"/>

     <xsl:template match="/">
          <table class="mainTable">
              <tr style="background:#f5f5f5;">
                  <th style="width:20%;">书名</th>
                  <th style="width:20%;">作者</th>
                  <th style="width:20%;">出版社</th>
                  <th style="width:20%;">出版日期</th>
                  <th style="width:20%;">定价</th>
              </tr>
              <xsl:for-each select="/BookStore/Book">
                  <xsl:element name="tr">
                      <xsl:element name="td">
                          <xsl:value-of select="Name" />
                      </xsl:element>
                      <xsl:element name="td">
                          <xsl:value-of select="Author" />
                      </xsl:element>
                      <xsl:element name="td">
                          <xsl:value-of select="Publisher" />
                      </xsl:element>
                      <xsl:element name="td">
                          <xsl:value-of
                      select="msxsl:format-date(PubDate, 'yyyy-M-dd')" />
                      </xsl:element>
                      <xsl:element name="td">
                          <xsl:value-of select="Price" />
                      </xsl:element>
                  </xsl:element>             
              </xsl:for-each>
          </table>
     </xsl:template>
 </xsl:stylesheet>

XSLT将XML转换为HTML


private static string ConvertToXhtml(string xml, string xslPath)
 {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml(xml);

     XslCompiledTransform transform = new XslCompiledTransform();
     transform.Load(xslPath);
     TextWriter writer = new StringWriter();
     transform.Transform(doc, null, writer);

     return writer.ToString();
 }

你可能感兴趣的:(xml,String,XSL)