Memory leak with XML Transform method

Transform XML with XSLT, return XmlDocument

I used to use XSLT to transform XML that I get from a database and write the output to a file:

//Transforms Xml with Xslt and write to file
protected void TransformXmlToFile(XmlDocument xmlDoc, 
                                    string xsltPath, 
                                    string transformedXmlPath)
{
    XslTransform transform = new XslTransform();
    XmlTextWriter xmlWriter = new XmlTextWriter(transformedXmlPath);
    
    transform.Load(xsltPath);
    xmlWriter.WriteStartDocument();
    transform.Transform(xmlDoc, Nothing, xmlWriter, Nothing);
    
    xmlWriter.Close();
}

After some testing we started to experience problems with this approach as there is a a memory leak in MS XML 4.0 core services while using transform.Transform() function.

 

There are two options

  1. Install XML 6.0 which has more secured and more stable or
  2. Change the way the function is written.

I decided to rather transform the XML and return a XmlDocument as the output… the solution is to use a MemoryStream for the output and then load that into your XmlDocument. Or create a string or whatever you want to return:  

 

//Transforms Xml with Xslt and returns the transformed XmlDocument
protected XmlDocument TransformXml(XmlDocument xmlDoc, string xsltPath)
{
    MemoryStream memStream = new MemoryStream();
    XslTransform transform  = new XslTransform();

    transform.Load(xsltPath);
    transform.Transform(xmlDoc, Nothing, memStream, Nothing);

    memStream.Seek(0, SeekOrigin.Begin);

    XmlDocument transformedDoc = new XmlDocument();
    return transformedDoc.Load(memStream);
}

 

Advertisements

Unzip files programmatically in C# – #ZipLib

 

So this was the first time I had to do this and I started to google… I was looking for a plain and simple way of unzipping a previously zipped file.

The results included:

  • System.IO.Compression that has been added in .NET 2.0 – used to compress and decompress streams. This didn’t exactly look like what I wanted and a lot of work.
  • WinRAR add-on that contained an UnRAR.dll for Windows. Michael A. McCloskey created a wrapper class for this which makes it much easier to use, but unfortunately I ran into troubles with .NET 2.0.
  • WinZip has a command line add-on which allows you to use batch files to extract the files from the zip, but a license for WinZip 10 Pro is needed.
  • #ziplib/SharpZipLib which was created by the guys from SharpDevelop. Entirely C#. This definitely looked like what I was looking for….

So I downloaded #ziplib and started looking at examples and jumped in and wrote some code… Created some GZipOutputStreams and FileStreams and tested and it didn’t work and I played around a bit more… During my searches I came across a post where someone mentioned FastZip, which is part of #ziplib and can be used for basic functions….

After a days searching and playing around and lines and lines of unnecessary code all I needed was the following:

 

using ICSharpCode.SharpZipLib.Zip;
FastZip fz =
new FastZip();       
fz.ExtractZip(zipFile, targetDirectory,"");

 

Very Cool. Is n’t it?