3

All,

I am not sure if this is possible so I thought to better ask here. In C# I am doing a lot of XSLT transformations of very big XML files (15MB each) The constant problem I am facing is the fact that XPathDocument uses way too much data to represent the XML in memory in a tree like format. Is it possible to use XmlReader which is SAX based to transform the document using XSLT?

Many Thanks,

MK

koumides
  • 2,464
  • 5
  • 34
  • 54
  • Yes it is possible to use XMLReader, rather it should be used for such processing. – Furqan Hameedi Mar 03 '11 at 10:37
  • @Furqan: Are there any examples I can look at? – koumides Mar 03 '11 at 10:47
  • I am afraid it can't be done with the standard .net framework classes. Read the answer of http://stackoverflow.com/questions/3101048/xslt-transformation-on-large-xml-files-with-c – Elian Ebbing Mar 03 '11 at 11:40
  • BizTalk had an implementation of Stream called [VirtualStream](http://technet.microsoft.com/en-us/library/microsoft.biztalk.streaming.virtualstream(BTS.70).aspx) which was was backed by disk. VirtualStream was intended to help keep memory management 'flatter' as objects 85K(?) in size are placed on the large object heap, which is swept during GC, but not compacted..leading to memory fragmentation and 'bloat'.. I don't know if there is a non-BizTalk related version out there anywhere tho. – Zach Bonham Mar 03 '11 at 22:20

4 Answers4

1

With XSLT 1.0 you can't avoid that the processor builds a tree model of the complete document and operates on it, that is how XSLT 1.0 is defined. In XSLT 3.0 you will be able to perform streaming transformations, there is already an early implementation of that in Saxon 9.3: http://www.saxonica.com/documentation/sourcedocs/streaming.xml.

On the other hand I don't understand why a "15MB" file is "very big". Are you really running out of memory when processing such a file with XSLT 1.0 on the .NET platform?

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

I would use the classes XPathNavigator (created from your XPathDocument with CreateNavigator) and XslCompiledTransform instead of XslTransform to perform the transformation. I think that the XPathNavigator works like a SAX parser and doesn't load the whole file into memory.

Hope this helps.

slfan
  • 8,950
  • 115
  • 65
  • 78
-1

There is a set of XSL controls available in the .net library which inherit from the the standard Xml controls.

An article is available here:

http://support.microsoft.com/kb/307322

using System;
using System.Xml;
using System.Xml.Xsl; 

namespace XSLTransformation
{
    public class Class1
    {
        static void Main(string[] args)
        {
            XslTransform myXslTransform = new XslTransform();

            myXslTransform.Load("reference.xsl"); 

            myXslTransform.Transform("inputfile.xml", "outputfile.xml");

         }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Pooli
  • 503
  • 5
  • 14
  • this is great as long as the inputfile.xml is loaded as XmlReader and not as XPathDocument or XmlDocument. Thanks, I will give it a try. – koumides Mar 03 '11 at 11:00
  • 1
    @koumides looks like I failed this a touch. Although the example i provided should work, the class and method are now obsolete. using System.Xml.Xsl.XslCompiledTransform should give you what you need to resolve this, and looks as though it can be passed an XmlReader and XmlWriter class as it's read and write parameters. – Pooli Mar 03 '11 at 11:17
-1

You can do like this ,

          XmlTextReader xtr = new XmlTextReader("C:\abc.xml");
          XPathDocument xp = new XPathDocument(xtr);
          XPathNavigator xpn =   xp.CreateNavigator();

Hope this shall get you going.

Furqan Hameedi
  • 4,372
  • 3
  • 27
  • 34