C#實(shí)現(xiàn)XSL轉(zhuǎn)換的方法
本文實(shí)例講述了C#實(shí)現(xiàn)XSL轉(zhuǎn)換的方法。分享給大家供大家參考,具體如下:
xsl 可方便的將一種格式的xml,轉(zhuǎn)換成另一種格式的xml,參考下面的代碼:
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Xsl; namespace XslLoad { class Program { static void Main(string[] args) { string xml = @"<?xml version='1.0' encoding='ISO-8859-1'?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> </catalog>"; string xsl = @"<?xml version='1.0' encoding='ISO-8859-1'?> <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match='/'> <html> <body> <h2>My CD Collection</h2> <table border='1'> <tr bgcolor='#9acd32'> <th align='left'>Title</th> <th align='left'>Artist</th> </tr> <xsl:for-each select='catalog/cd'> <tr> <td><xsl:value-of select='title'/></td> <td><xsl:value-of select='artist'/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>"; string result = XslTransform(xml, xsl); Console.WriteLine(result); Console.Read(); } /// <summary> /// 將Xml利用Xsl轉(zhuǎn)換成目標(biāo)xml /// </summary> /// <param name="inputXmlConent">輸入的xml</param> /// <param name="inuptXslContent">xsl</param> /// <returns>轉(zhuǎn)換后的目標(biāo)xml</returns> static String XslTransform(string inputXmlConent, string inuptXslContent) { XmlReader readerXml = XmlReader.Create(new MemoryStream(UTF8Encoding.UTF8.GetBytes(inputXmlConent))); XmlReader readerXsl = XmlReader.Create(new MemoryStream(UTF8Encoding.UTF8.GetBytes(inuptXslContent))); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(readerXsl); StringBuilder sb = new StringBuilder(); XmlWriterSettings Settings = new XmlWriterSettings() { Indent = true, ConformanceLevel = ConformanceLevel.Auto }; XmlWriter writer = XmlWriter.Create(sb, Settings); transform.Transform(readerXml, writer); return sb.ToString(); } } }
輸出結(jié)果:
<html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <tr> <td>Empire Burlesque</td> <td>Bob Dylan</td> </tr> </table> </body> </html>
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
手把手教你如何基于C#制作一個(gè)網(wǎng)址檢測(cè)工具
這篇文章主要給大家介紹了關(guān)于如何基于C#制作一個(gè)網(wǎng)址檢測(cè)工具的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02C#多線程學(xué)習(xí)之(一)多線程的相關(guān)概念分析
這篇文章主要介紹了C#多線程學(xué)習(xí)之多線程的相關(guān)概念,涉及C#中多線程的相關(guān)概念與使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#生成指定范圍內(nèi)的不重復(fù)隨機(jī)數(shù)
對(duì)于隨機(jī)數(shù),大家都知道,計(jì)算機(jī)不 可能產(chǎn)生完全隨機(jī)的數(shù)字,所謂的隨機(jī)數(shù)發(fā)生器都是通過一定的算法對(duì)事先選定的隨機(jī)種子做復(fù)雜的運(yùn)算,用產(chǎn)生的結(jié)果來近似的模擬完全隨機(jī)數(shù),這種隨機(jī)數(shù)被稱 作偽隨機(jī)數(shù)。偽隨機(jī)數(shù)是以相同的概率從一組有限的數(shù)字中選取的。2015-05-05WebService 的簡(jiǎn)單封裝接口調(diào)用方法
這篇文章主要介紹了WebService 的簡(jiǎn)單封裝接口調(diào)用方法,主要是通過簡(jiǎn)單的sql語句來查詢數(shù)據(jù)庫,從而返回dataset,十分簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06WPF使用DrawingContext實(shí)現(xiàn)二維繪圖
這篇文章介紹了WPF使用DrawingContext實(shí)現(xiàn)二維繪圖的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06