What kind of errors to expect in XSLT using C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaurangs
    New Member
    • Sep 2010
    • 1

    #1

    What kind of errors to expect in XSLT using C#?

    Hi all,

    I am writing a code snippet for a website in ASP.NET

    I need to accept an inputXML in memorystream object, and perform XSLT on it.

    If the transform is successful, I need to output the converted XML in stream, otherwise, display the errors encountered.

    I have started learning .NET recently, so I am not very well versed with it.

    What kind of errors should I expect and how do I catch them?

    Any suggestions to improve my code will be appreciated.

    Thanks.


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Xml;
    using System.Xml.Xsl;
    using System.Text;
    using System.Xml.XPath;
    using System.Diagnostics;
    
    
    public partial class _Default : System.Web.UI.Page
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
            XmlReader xml = LoadXML();
            XmlReader xsl = LoadXSL();
           
            //Create a stream to write to
            MemoryStream myStreamWriter = new MemoryStream();
            XmlTextWriter mytw = new XmlTextWriter(myStreamWriter, Encoding.UTF8);
            XMLTransform(xsl, xml, mytw);
        }   
    
        private XmlReader LoadXML()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            return XmlReader.Create("C:\\...\\input.xml", settings);
        }
    
        private XmlReader LoadXSL()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            return XmlReader.Create("C:\\..\\CQCal.xsl", settings);
        }
    
        private void XMLTransform(XmlReader xslt, XmlReader inputxml, XmlTextWriter outputxml)
        {
            TextWriter outputtext = new StreamWriter("C:\\..\\outputxml.txt");
            System.Xml.Xsl.XslCompiledTransform xslTransform = new System.Xml.Xsl.XslCompiledTransform();
            xslTransform.Load(xslt);
            
            xslTransform.Transform(inputxml, outputxml);
    
            //This part checks the output in a file, as I was unsure how to check whether the transform had been performed correctly.
            xslTransform.Transform(inputxml, null, outputtext);
            outputtext.Write(outputtext.ToString());
        }
    }
  • ZiadElmalki
    New Member
    • Sep 2010
    • 43

    #2
    If you hover you mouse over the methods in Visual Studio the intellisense documentation will tell you which exceptions are thrown. You can write code to handle those.

    I would imagine you have these possible issues.

    • Cannot open the xml file. File is locked, doesn't exist, etc.
    • Cannot open the xslt file. File is locked, doesn't exist, etc.
    • The xml file is malformed.
    • The xslt file is malformed.
    • There was some error while executing the xslt.


    Look at you could and put some try catches in your code when you are loading both of the files and one where you are actually executing the xslt. You can use multiple catch block to determine the exception type.

    Comment

    Working...