在 C# 中使用 XSLT 将 XML 转换为 HTML 的最简单方法? [英] Simplest way to transform XML to HTML with XSLT in C#?
本文介绍了在 C# 中使用 XSLT 将 XML 转换为 HTML 的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XSLT 新手问题:请填写下面 C# 代码片段中的空白:
XSLT newbie question: Please fill in the blank in the C# code fragment below:
public static string TransformXMLToHTML(string inputXml, string xsltString) {
// insert code here to apply the transform specified by xsltString to inputXml
// and return the resultant HTML string.
// You may assume that the xslt output type is HTML.
}
谢谢!
推荐答案
怎么样:
public static string TransformXMLToHTML(string inputXml, string xsltString)
{
XslCompiledTransform transform = new XslCompiledTransform();
using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) {
transform.Load(reader);
}
StringWriter results = new StringWriter();
using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) {
transform.Transform(reader, null, results);
}
return results.ToString();
}
请注意,理想情况下,您应该缓存并重新使用 XslCompiledTransform
- 或者使用 XslTransform
代替(尽管它被标记为已弃用).
Note that ideally you would cache and re-use the XslCompiledTransform
- or perhaps use XslTransform
instead (it is marked as deprecated, though).
这篇关于在 C# 中使用 XSLT 将 XML 转换为 HTML 的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文