我要寻找的.NET框架,将XML片段和XSLT文件的静态函数,应用转型在内存中,并返回转换后的XML。
I am looking for a static function in the .NET framework which takes an XML snippet and an XSLT file, applies the transformation in memory, and returns the transformed XML.
我想做到这一点:
string rawXml = invoiceTemplateDoc.MainDocumentPart.Document.InnerXml;
rawXml = DoXsltTransformation(rawXml, @"c:\prepare-invoice.xslt"));
// ... do more manipulations on the rawXml
替代地,而不是采取并返回字符串,它可以被取和返回的XMLNodes
Alternatively, instead of taking and returning strings, it could be taking and returning XmlNodes.
有没有这样的功能?
一个知之甚少的特点是,你实际上可以直接将数据转换为的XmlDocument
DOM或成LINQ到XML 的XElement
或的XDocument
(通过 CreateWriter()
方法),而无需通过获取的XmlWriter
实例与数据养活他们通过文字的形式。
A little know feature is that you can in fact transform data directly into a XmlDocument
DOM or into a LINQ-to-XML XElement
or XDocument
(via the CreateWriter()
method) without having to pass through a text form by getting an XmlWriter
instance to feed them with data.
假设你的XML输入 IXPathNavigable
并且您已经加载了 XslCompiledTransform
例如,你可以做以下
Assuming that your XML input is IXPathNavigable
and that you have loaded a XslCompiledTransform
instance, you can do the following:
XmlDocument target = new XmlDocument(input.CreateNavigator().NameTable);
using (XmlWriter writer = target.CreateNavigator().AppendChild()) {
transform.Transform(input, writer);
}
您然后到 taget
文档转换的文档。请注意,也有其他重载的转换
,允许你通过XSLT参数和扩展到样式表。
You then have the transformed document in the taget
document. Note that there are other overloads on the transform
to allow you to pass XSLT arguments and extensions into the stylesheet.
。然而,它可能是一个好主意,缓存变换自加载和编译它不是免费
If you want you can write your own static helper or extension method to perform the transform as you need it. However, it may be a good idea to cache the transform since loading and compiling it is not free.