xsl 可方便的将一种格式的xml,转换成另一种格式的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'?>
Empire BurlesqueBob Dylan
USA
Columbia
10.90
1985
";
string xsl = @"<?xml version='1.0' encoding='ISO-8859-1'?>
My CD Collection
Title | Artist |
---|---|
";
string result = XslTransform(xml, xsl);
Console.WriteLine(result);
Console.Read();
}
///
/// 将Xml利用Xsl转换成目标xml
///
/// 输入的xml
/// xsl
/// 转换后的目标xml
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();
}
}
}
输出结果:
My CD Collection
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |