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#實(shí)現(xiàn)去除Strings中空格的方法
這篇文章主要介紹了C#實(shí)現(xiàn)去除Strings中空格的方法,較為詳細(xì)的介紹了C#實(shí)現(xiàn)去除字符串首尾及中間空格的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10
c#?使用線程對(duì)串口serialPort進(jìn)行收發(fā)數(shù)據(jù)(四種)
本文主要介紹了c#?使用線程對(duì)串口serialPort進(jìn)行收發(fā)數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C#數(shù)據(jù)庫(kù)操作之LINQ to SQL技術(shù)詳解
本文詳細(xì)介紹了LINQtoSQL技術(shù),包括其基本概念、使用方法、動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫(kù)、插入數(shù)據(jù)、查詢數(shù)據(jù)、更新數(shù)據(jù)和刪除數(shù)據(jù)等操作2024-12-12
C#中BitmapImage與BitmapSource接口的區(qū)別對(duì)比小結(jié)
BitmapImage和BitmapSource都可以用于表示和顯示圖像,本文就來(lái)介紹一下C#中BitmapImage與BitmapSource接口的區(qū)別對(duì)比,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
C#中實(shí)現(xiàn)可變參數(shù)實(shí)例
這篇文章主要介紹了C#中實(shí)現(xiàn)可變參數(shù)實(shí)例,本文演示使用params 實(shí)現(xiàn)可變數(shù)量的參數(shù),并且這些參數(shù)的類型可以不同,需要的朋友可以參考下2015-01-01

