Solved

XSLT optimization Techniques in .NET 3.5

Posted on 2009-05-08
6
642 Views
Last Modified: 2013-11-18
I'm trying to perform xslt transformation on a serilaized version of object.
As a matter of fact I need to apply the same transformation on collection of objects looping thru each of them at once.
Is there a way to perform this transformation in a best way possible using new classes .net 3.5 or any(I'm using .net 3.5) ?
For proof of concept I have attached the code below
 
temp - Instance of an object I'm trying to serialize
 
            
            XmlReaderSettings rs = new XmlReaderSettings();
            rs.ProhibitDtd = false;
            
            string xsltPath = "temp.xslt";
            XmlReader reader = XmlReader.Create(xsltPath, rs);
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(reader);
 
            MemoryStream xmlStream = new MemoryStream();
 
            for (int i = 0; i < 10000; i++)
            {
 
                string xml = serialize(temp);
 
                try
                {
                    string result;
 
                    byte[] xmldata = Encoding.ASCII.GetBytes(xml);
                    xmlStream = new MemoryStream(xmldata);
 
                    xmlStream.Position = 0;
 
                    XsltArgumentList argList = new XsltArgumentList();                   
 
                    
                    XPathDocument xmlDoc = new XPathDocument(xmlStream);
                    xmlStream = new MemoryStream();
                    xslt.Transform(xmlDoc, argList, xmlStream);
                    xmlStream.Position = 0;
                    
                    using (StreamReader r = new StreamReader(xmlStream))
                    {
                        result = r.ReadToEnd();
                    }
 
                }
                catch (XsltException ex)
                {
                    throw new ArgumentException();
                }
                finally
                {
                    reader.Close();
                    xmlStream.Close();
                }
            }

Open in new window

0
Comment
Question by:poojanair
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24391133
The largest performance hit is already outside your loop, the compilation of the XSLT stylesheet. I can't judge from here, without seeing the actual XSLT and the actual XML whether the XSLT is performant (though that highly depends on the different processors and you are lucky, Microsoft's .NET processors are amongst the fastest available).

Now, there's also another technique you may consider and that may speed it up. The idea is simple: make sure to load the XSLT only once. There are two ways that are worth investigating whether that speeds up your program:

1. combine all small XML files into one larger and give that to the XSLT. If all XML together do not exceed, say, 20MB, you should be fine and it may outperform what you currently have.

2. use a small extra XML that contains all the filenames (or external calls through an UriResolver which map to the in-memory data), this can be a in-memory dom just as you have now. Pass it on as source and the XSLT can use document() function for retrieving of the data.

Esp. the second one will be quite some work. The good thing about the second one is that you stay inside the XSLT and that you do not have the overhead of restarting a transformation. Using small documents is often more efficient than one larger one, but only timings will find out.

How complex is your serialized type? Can you show your XSLT?
0
 

Author Comment

by:poojanair
ID: 24454361
Thanks for the info.

Asssuming my  xml and xslt are fairly simple my question is , are there any new classes in .net 3.5 that wud speed up the process when you are applying the same transformation repeatedly.
I tried using XmlSerializerFactory but that does not speed up the process neither does XmlSerializerCache.
0
 
LVL 39

Expert Comment

by:abel
ID: 24454494
If your XML and XSLT are fairly simple and speed is of the utmost importance to you, you should not use XSLT at all. Instead, use the XmlTextReader. Using that, you won't consume any memory (other then some temporary strings) and it is blazingly fast. The technique for XML processing is called the Pull-model, and the idea is that you tell the XmlTextReader how much it can skip to the next node.

This only works in streaming situations, where the data can be read forward-only, and where not too many memories have to be made about previous nodes visited. I.e., if you have complex XPaths, I shouldn't go down that path.

I can help you setup a principle system though.
0
 

Author Comment

by:poojanair
ID: 24552952
My xslt is not that simple that I can skip it.But I would  be glad if you can provide a small example on how to use an XMLTextReader to do the job here(assuming the xslt is simple).
0
 
LVL 39

Accepted Solution

by:
abel earned 250 total points
ID: 24553991
I used this technique for answering this question, look up the code snippet, it shows some ways to use XmlTextReader: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24333151.html#24180827

The basic idea with XmlTextReader is fairly simple and straightforward. You open an XML file and you tell the reader how much you want to skip. A good "friend" while doing so is ReadToFollowing("elementname"). The largest downside of this approach is that back/forward looking (which is common with XPath) cannot be done so easily and should be avoided if possible. It is, however, lightning fast and outranks even SAX2 in speed.

XmlTextReader reader = new XmlTextReader("Data/yourdatfile.xml");
 
// bring readpointer to next node-part (begin node, end node)
while (reader.Read())
{
    // reads up to the element
    reader.ReadToFollowing("SomeElementName");
 
    // reads up to the end of the element, reading its contents as string
    Debug.Write(reader.ReadString().Trim());
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question