如何在 C# 中应用 XSLT 样式表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply an XSLT Stylesheet in C#
提问by Daren Thomas
I want to apply an XSLT Stylesheet to an XML Document using C# and write the output to a File.
我想使用 C# 将 XSLT 样式表应用于 XML 文档并将输出写入文件。
采纳答案by Daren Thomas
I found a possible answer here: http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
我在这里找到了一个可能的答案:http: //web.archive.org/web/20130329123237/http: //www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
From the article:
从文章:
XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
myXslTrans.Transform(myXPathDoc,null,myWriter) ;
Edit:
编辑:
But my trusty compiler says, XslTransform
is obsolete: Use XslCompiledTransform
instead:
但是我可信赖的编译器说,XslTransform
已经过时了:XslCompiledTransform
改用:
XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,null,myWriter);
回答by ManBugra
Here is a tutorial about how to do XSL Transformations in C# on MSDN:
这是有关如何在 MSDN 上的 C# 中进行 XSL 转换的教程:
http://support.microsoft.com/kb/307322/en-us/
http://support.microsoft.com/kb/307322/en-us/
and here how to write files:
和这里如何写文件:
http://support.microsoft.com/kb/816149/en-us
http://support.microsoft.com/kb/816149/en-us
just as a side note: if you want to do validation too here is another tutorial (for DTD, XDR, and XSD (=Schema)):
顺便提一下:如果您也想进行验证,这里是另一个教程(适用于 DTD、XDR 和 XSD(=Schema)):
http://support.microsoft.com/kb/307379/en-us/
http://support.microsoft.com/kb/307379/en-us/
i added this just to provide some more information.
我添加这个只是为了提供更多信息。
回答by Heinzi
Based on Daren's excellent answer, note that this code can be shortened significantly by using the appropriate XslCompiledTransform.Transform overload:
基于 Daren 的出色回答,请注意,通过使用适当的XslCompiledTransform.Transform 重载可以显着缩短此代码:
var myXslTrans = new XslCompiledTransform();
myXslTrans.Load("stylesheet.xsl");
myXslTrans.Transform("source.xml", "result.html");
(Sorry for posing this as an answer, but the code block
support in comments is rather limited.)
(很抱歉将其视为答案,但code block
评论中的支持相当有限。)
In VB.NET, you don't even need a variable:
在 VB.NET 中,您甚至不需要变量:
With New XslCompiledTransform()
.Load("stylesheet.xsl")
.Transform("source.xml", "result.html")
End With
回答by Vinod Srivastav
This might help you
这可能会帮助你
public static string TransformDocument(string doc, string stylesheetPath)
{
Func<string,XmlDocument> GetXmlDocument = (xmlContent) =>
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlContent);
return xmlDocument;
};
try
{
var document = GetXmlDocument(doc);
var style = GetXmlDocument(File.ReadAllText(stylesheetPath));
System.Xml.Xsl.XslCompiledTransform transform = new System.Xml.Xsl.XslCompiledTransform();
transform.Load(style); // compiled stylesheet
System.IO.StringWriter writer = new System.IO.StringWriter();
XmlReader xmlReadB = new XmlTextReader(new StringReader(document.DocumentElement.OuterXml));
transform.Transform(xmlReadB, null, writer);
return writer.ToString();
}
catch (Exception ex)
{
throw ex;
}
}