开发者

How to detect the error raised on a particular codeline?

开发者 https://www.devze.com 2022-12-31 11:06 出处:网络
[Please edit the title if you find its not good enough] I have code which triggers XSL-transformation:

[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.


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.


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); 
0

精彩评论

暂无评论...
验证码 换一张
取 消