Optimizing XDocument for XDocument XSLT

The following code works, but is dirty and slow. I will convert an XDocument to another XDocument using XSLT2 with a saxophone, adapted using 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;
}

I understand that there can be slowness in bits for which I have no control, but are there any ways to do all the switching between XDocument and XmlDocument and using writers?

+3
source share
2 answers

Instead of using strings to create an XDocument, you can try directly passing the XmlWriter created from the XDocument:

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

In addition, other slowdowns are probably in SaxonWrapper itself, and it uses the old XmlDocument, rather than a faster cousin.

+2

eddiegroves . , , . , :

XDocument outputDoc = new XDocument();
using (var writer = outputDoc.CreateWriter()) {
    processor.Transform(sourceDoc, writer);
}
return outputDoc;

, - , , - .

+4

Source: https://habr.com/ru/post/1724485/


All Articles