应用XSLT在内存中的XML和返回内存中XML [英] Apply XSLT on in-memory XML and returning in-memory XML

查看:167
本文介绍了应用XSLT在内存中的XML和返回内存中XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找的.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.

这篇关于应用XSLT在内存中的XML和返回内存中XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆