tags:

views:

81

answers:

2

[Please edit the title if you find its not good enough]

I have code which triggers XSL-transformation:

objMemoryStream = new MemoryStream();

xslTransform = new XslCompiledTransform();
xpathXmlOrig = new XPathDocument("E:\\xslt error\\Simulation_of_error\\input.xml");

xslSettings = new XsltSettings();
xslSettings.EnableScript = true;
xslTransform.Load(strXmlQueryTransformPath, xslSettings, new XmlUrlResolver());
xslTransform.Transform(xpathXmlOrig, null, objMemoryStream);
objMemoryStream.Position = 0;
StreamReader objStreamReader = new StreamReader(objMemoryStream);

The method xslTransform.Load(strXmlQueryTransformPath, xslSettings, new XmlUrlResolver()); is a victim, which fails some times due to some time-out issue.

I want to detect the failure of this codeline and execute again until it successfully executes!

I tried using "TRY CATCH and WHILE methods":

bool flag = true;
do
{
    try
    {
        xslTransform.Load(strXmlQueryTransformPath, xslSettings, new XmlUrlResolver());
        flag = false;
    }
    catch
    {
        flag = true;
    }
} while (flag);

but the problem is "error is getting logged in the log file", Well. The whole code is under one more try statement, which I suspect is writing to log. Which is what I don't want... I don't want end user to know about the failure of this codeline.

Is there anyway to get it done?

The appearance of error is completely Random. First time when it fails, I try to retrigger the code, which may result in the successful transformation (on next attempt)! This is the reason why I came to conclusion that recall of Load() method would fix the problem.

+1  A: 

Did you try to remove the inline scripts and to pass an extension object to the transformation?

I believe this will most probably solve the problem.

Otherwise you should catch XsltException and its properties LineNumber and LinePosition give you the location in the code where the exception happened.

Update: A simple example of writing an extension function (part of an extension object) passed to the transformation, and its usage within the XSLT transformation is provided here.

Dimitre Novatchev
Actually, the dll files are created while the codeline xslTransform.Load() method is used.In the server many other code snippets are running, which may dominate this process, so the execution of the process takes long time, on the other hand the dll files are deleted(may be because of some time out issue) which makes the function (whose execution was incomplete) pop ups error because it fails to find the *.dll (also some *.tmp) files. So I concluded that the re-usage of xslCompliledTransfom.Load() will fix the problem.. but unfortunately. it also pops up error before executing successfully :(
infant programmer
As I use C# scripting inside the xslt code (as you can find in the sample code), which requires xslCompliteTransform to create dll files to execute xslCompiledTransform.Load() method.And the notable thing about server performance is : It doesn't trigger this error everytime :! the retriggering of transformation does work finely .. !which inturn drives to the confusion ..
infant programmer
@infant-programmer: So, allthemore, this means: remove the inline scripts and make them normal extension functions. Just do it and you'll eliminate this problem.
Dimitre Novatchev
@Dimitre, please excuse me sir, I need some guidance on this .. I am not understanding how and what way I can use extension functions.. As I am not a pro in xslt, I need some sample code etc .. to understand.
infant programmer
@infant-programmer: You are always welcome :). I updated my answer with a link to a code example how to specify/add and then transform and use an extension function within the XSLT transformation.
Dimitre Novatchev
@Dimitre, Looks pretty nice .. just have to see .. whether its possible to edit the triggering code .. as it is kind of read-only.
infant programmer
@infant-programmer: the only edits should be the removal of the inline scripts. You'll use the same namespace for the extension object as the one already declared on the xslt stylesheet. Also, you'll define your methods to have the exact names and signatures as those of the inline scripts.
Dimitre Novatchev
@Dimitre, you nailed it .. thanks a million for helping me .. :))))
infant programmer
@infant-programmer: I just like answering intelligent questions :) So you are always most welcome.
Dimitre Novatchev
A: 

try using one of the Constructor overloads. It will allow you to step though your transform.

//public XslCompiledTransform(bool enableDebug);
var xslTransform = new XslCompiledTransform(true); 
Matthew Whited
@matthew, ya I have tried it .. the problem being posted here is the conclusion drawn from debugging ..
infant programmer