加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 优化XDocument到XDocument XSLT

发布时间:2020-12-16 01:33:32 所属栏目:百科 来源:网络整理
导读:以下代码可以正常工作,但是很麻烦而且很慢.我正在使用带有Saxon的XSLT2将XDocument转换为另一个XDocument,使用SaxonWrapper进行调整: public static XDocument HSRTransform(XDocument source){ System.Reflection.Assembly thisExe = System.Reflection.As
以下代码可以正常工作,但是很麻烦而且很慢.我正在使用带有Saxon的XSLT2将XDocument转换为另一个XDocument,使用SaxonWrapper进行调整:

public static XDocument HSRTransform(XDocument source)
{
    System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.Stream xslfile = thisExe.GetManifestResourceStream("C2KDataTransform.Resources.hsr.xsl");

    XmlDocument xslDoc = new XmlDocument();
    xslDoc.Load(xslfile);

    XmlDocument sourceDoc = new XmlDocument();
    sourceDoc.Load(source.CreateReader());

    var sw = new StringWriter();

    Xsl2Processor processor = new Xsl2Processor();
    processor.Load(xslDoc);

    processor.Transform(sourceDoc,new XmlTextWriter(sw));

    XDocument outputDoc = XDocument.Parse(sw.ToString());
    return outputDoc;
}

我意识到缓慢可能实际上是我无法控制的位,但是有更好的方法来完成XDocument和XmlDocument之间的所有切换以及编写器的使用吗?

解决方法

您可以尝试直接传入从XDocument创建的XmlWriter,而不是使用字符串来创建XDocument:

XDocument outputDoc = new XDocument();
processor.Transform(sourceDoc,outputDoc.CreateWriter());
return outputDoc;

除此之外,其他减速可能在SaxonWrapper本身,它使用旧的XmlDocument – 而不是它的表现速度更快.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读