I have modified the title of the question after finding the answer 🙂 😛
I am loading an XML file and an XSL file by a C# program and triggering the XSL transformation .. here is the code for it:
static void Main(string[] args)
{
// Create the XslCompiledTransform and load the stylesheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("input.xsl"); //located in Debug folder
//Load XSL argument list
XsltArgumentList xslArg = new XsltArgumentList();
// Transform the file.
using (XmlWriter w = XmlWriter.Create("output.xml"))
{
xslt.Transform("input.xml", xslArg, w); //located in Debug folder
}
}
The error is I am not able to load XML file.
The XSL file contains some C# code which is meant to calculate the difference between two DateTime strings .. well, I can transform the XML file manually using the same XSL file .. But when I try to trigger the transformation using C# code .. then it says “It can’t load XML file”
Here is my (part of) XSL code ..
<span bgcolor="#EEEEEE">
<xsl:variable name="date1" select="//date1"/>
<xsl:variable name="date2" select="//date2"/>
<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
public string datediff(string date1, string date2) {
DateTime startTime = new DateTime(Convert.ToInt32(date1.Substring(6, 4)), Convert.ToInt32(date1.Substring(0, 2)), Convert.ToInt32(date1.Substring(3, 2)), Convert.ToInt32(date1.Substring(11, 2)), Convert.ToInt32(date1.Substring(14, 2)), Convert.ToInt32(date1.Substring(17, 2)), 0);
DateTime endTime = new DateTime(Convert.ToInt32(date2.Substring(6, 4)), Convert.ToInt32(date2.Substring(0, 2)), Convert.ToInt32(date2.Substring(3, 2)), Convert.ToInt32(date2.Substring(11, 2)), Convert.ToInt32(date2.Substring(14, 2)), Convert.ToInt32(date2.Substring(17, 2)), 0);
return(endTime.Subtract(startTime));
}
]]>
</msxsl:script><br>
<xsl:template match="datediff"><br>
<xsl:element name="{local-name()}"><br>
<xsl:value-of select="cs:datediff($date1, $date2)" /><br>
</xsl:element><br>
</xsl:template><br></span>
Is that, because of script(C# code to calculate date diff..) I am getting this error?
By the way C# code runs perfectly when I use some other input XML and XSL files ..
please help me to overcome this error ..
As Steve cooper has mentioned .. you need to enable the XSLT script .. and here is the way to do it:
first define a new
settingsinstance:then enable the script
Create the
XslCompiledTransformobject and load the style sheet, passing in thesettingsobject.