Transforming large Xml files

Posted by Chad on Stack Overflow See other posts from Stack Overflow or by Chad
Published on 2010-05-21T19:00:44Z Indexed on 2010/05/21 19:10 UTC
Read the original article Hit count: 205

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml