在C#asp.net中加载XSLT时出错

时间:2016-01-25 12:27:16

标签: c# asp.net xml xslt

当我尝试加载XSLT时,我收到以下错误 的 Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

string xmlFilePath = Path.Combine(GetAssemblyDirectory(), "TestResult.xml");
string xslFilePath = Path.Combine(GetAssemblyDirectory(), "SingleTableTestResult.xslt");
 strResultSummary = strResultSummary.Replace(ProjectPath, ProjectName);
File.WriteAllText(xmlFilePath, strResultSummary, System.Text.Encoding.UTF8);

//get the formatted HTML Report tranformed via xslt
string reportFileData = GenerateTestReport(xmlFilePath, xslFilePath);

//gets the path of the running assembly directory
private static string GetAssemblyDirectory()
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            DashboardBaseLogger.dashBoardlogger.WriteInfo("Directory location: " + Path.GetDirectoryName(path));
            return Path.GetDirectoryName(path);
        }

//Generates the test report
private string GenerateTestReport(string XMLFilePath, string XSLFilePath)
        {
            string reportFilePath = string.Empty;
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(XSLFilePath); //Exception here
            reportFilePath = Path.Combine(GetAssemblyDirectory(), "TestResult.html");
            transform.Transform(XMLFilePath, reportFilePath);
            return reportFilePath;          
        }

在此处获取例外transform.Load(XSLFilePath); //Getting an Exception here

有人可以帮我解决这个问题吗? 任何帮助将不胜感激。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个,看看它是否有所作为。

   // Create the XsltSettings object with script enabled.
    XsltSettings settings = new XsltSettings(false,true);//By default, the XslCompiledTransform class disables support for the XSLT document() function and embedded scripting. This is a way to bypass it.
    XslCompiledTransform transform = new XslCompiledTransform();
    transform.Load(XSLFilePath, settings, new XmlUrlResolver() ); //Exception here
    reportFilePath = Path.Combine(GetAssemblyDirectory(), "TestResult.html");
    transform.Transform(XMLFilePath, reportFilePath);
    return reportFilePath;  

此处提供更多信息。 https://msdn.microsoft.com/en-us/library/66f54faw%28v=vs.110%29.aspx