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.
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());
}
}
Comment