tags:

views:

576

answers:

3

I have modified the title of the question after finding the answer :) :P

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

<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>
<xsl:template match="datediff">
<xsl:element name="{local-name()}">
<xsl:value-of select="cs:datediff($date1, $date2)" />
</xsl:element>
</xsl:template>

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

+2  A: 

I would suggest trying to load the XML file completely separately - I wouldn't be surprised to find that this had nothing at all to do with XSL, and everything to do with it not being able to find the file or something similar.

Try loading the XML file into an XmlDocument and checking that it looks correct. If that works, use the overload accepting an IXPathNavigable as input (XmlDocument implements IXPathNavigable).

Jon Skeet
+3  A: 

In the MSDN Documentation it says "XSLT scripting is disabled by default. XSLT scripting should be enabled only if you require script support and you are working in a fully trusted environment."

This is probably your problem. Try loading the transform like this;

XslCompiledTransform xslt = new XslCompiledTransform();

// Disable script blocks and the document() function
// if a stylesheet came from an untrusted source
string untrustedUri = @"http://www.untrusted-site.com/meow.xsl";
XmlResolver secureResolver = new XmlSecureResolver(new XmlUrlResolver(), untrustedUri);
xslt.Load(untrustedUri, XsltSettings.Default, secureResolver);

// Enable script blocks and the document() function
// if a trusted stylesheet needs them
xslt.Load(@"C:\MyProject\purr.xsl", XsltSettings.TrustedXslt, new XmlUrlResolver());

You could add some detail to your question, too; can you say how you are able to do it manually? What program or engine are you using? For instance, XMLSpy uses a different transform engine from the .Net framework, so XSL files can be incompatable.

Steve Cooper
Can clarify it giving more detail .. how exactly can I enable script ..corresponding useful links would be helpful ..
infant programmer
When I tried the same code that you have posted I am getting error..there is no guide to explain, what exactly is that XsltSetting.TrustedXslt is meant for .. may be some required code line is missing between the lines ..
infant programmer
+2  A: 

As Steve cooper has mentioned .. you need to enable the XSLT script .. and here is the way to do it ..

first define a new settings

       XsltSettings settings = new XsltSettings;

then enable the script

settings.EnableScript = true;

Create the XslCompiledTransform object and load the style sheet.

infant programmer