C#實(shí)現(xiàn)XSL轉(zhuǎn)換的方法
更新時間:2015年11月27日 11:47:54 作者:Jimmy.Yang
這篇文章主要介紹了C#實(shí)現(xiàn)XSL轉(zhuǎn)換的方法,結(jié)合實(shí)例分析了C#執(zhí)行XSL轉(zhuǎn)換XML的相關(guā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>
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C#多線程學(xué)習(xí)之(一)多線程的相關(guān)概念分析
這篇文章主要介紹了C#多線程學(xué)習(xí)之多線程的相關(guān)概念,涉及C#中多線程的相關(guān)概念與使用技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
C#生成指定范圍內(nèi)的不重復(fù)隨機(jī)數(shù)
對于隨機(jī)數(shù),大家都知道,計算機(jī)不 可能產(chǎn)生完全隨機(jī)的數(shù)字,所謂的隨機(jī)數(shù)發(fā)生器都是通過一定的算法對事先選定的隨機(jī)種子做復(fù)雜的運(yùn)算,用產(chǎn)生的結(jié)果來近似的模擬完全隨機(jī)數(shù),這種隨機(jī)數(shù)被稱 作偽隨機(jī)數(shù)。偽隨機(jī)數(shù)是以相同的概率從一組有限的數(shù)字中選取的。2015-05-05
WPF使用DrawingContext實(shí)現(xiàn)二維繪圖
這篇文章介紹了WPF使用DrawingContext實(shí)現(xiàn)二維繪圖的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

