Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming large Xml files

I was using this extension method to transform very large xml files with an xslt.

Unfortunately, I get an OutOfMemoryException on the source.ToString() line.

I realize there must be a better way, I'm just not sure what that would be?

public static XElement Transform(this XElement source, string xslPath, XsltArgumentList arguments)
{
        var doc = new XmlDocument();
        doc.LoadXml(source.ToString());

        var xsl = new XslCompiledTransform();
        xsl.Load(xslPath);

        using (var swDocument = new StringWriter(System.Globalization.CultureInfo.InvariantCulture))
        {
            using (var xtw = new XmlTextWriter(swDocument))
            {
                xsl.Transform((doc.CreateNavigator()), arguments, xtw);
                xtw.Flush();
                return XElement.Parse(swDocument.ToString());
            }
        }
}

Thoughts? Solutions? Etc.

UPDATE: Now that this is solved, I have issues with validating the schema! Validating large Xml files

like image 409
CaffGeek Avatar asked Jan 23 '23 01:01

CaffGeek


2 Answers

Try this:

using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;

static class Extensions
{
    public static XElement Transform(
        this XElement source, string xslPath, XsltArgumentList arguments)
    {
        var xsl = new XslCompiledTransform();
        xsl.Load(xslPath);

        var result = new XDocument();
        using (var writer = result.CreateWriter())
        {
            xsl.Transform(source.CreateNavigator(), arguments, writer);
        }
        return result.Root;
    }
}

BTW, new XmlTextWriter() is deprecated as of .NET 2.0. Use XmlWriter.Create() instead. Same with new XmlTextReader() and XmlReader.Create().

like image 116
John Saunders Avatar answered Jan 24 '23 13:01

John Saunders


For large XML files you can try to use XPathDocument as suggested in Microsoft Knowledge Base article.

XPathDocument srcDoc = new XPathDocument(srcFile);
XslCompiledTransform myXslTransform = new XslCompiledTransform();
myXslTransform.Load(xslFile);
using (XmlWriter destDoc = XmlWriter.Create(destFile))
{
    myXslTransform.Transform(srcDoc, destDoc);
}
like image 37
Vitaliy Ulantikov Avatar answered Jan 24 '23 13:01

Vitaliy Ulantikov