Click here to Skip to main content
Click here to Skip to main content
Technical Blog

All about XML transformation using XSLT and C#

, 13 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
XML is a universal format for data exchange, most of the data repository owners store their data in xml format, and apply XSLT to transform the XML from one format to another when required, I came across to a similar project, where we were storing everything in XML and then while processing the data

XML is a universal format for data exchange, most of the data repository owners store their data in xml format, and apply XSLT to transform the XML from one format to another when required, I came across to a similar project, where we were storing everything in XML and then while processing the data we needed to display in other format.

Have written my own small transformation utility with extension object support.

Transform method:

/// <summary>
/// perform the XSLT transformation on the input xml and provides the transformed xml
/// </summary>
/// <param name=”transformation”>contains input xml, xslt path, and output xml</param>
public static void Transform(ITransformation transformation)
{
    string sourceXML = transformation.SourceXML;
    
    using (XmlReader reader = XmlReader.Create(new StringReader(sourceXML)))
    {
        //instead of initializing it with a new instance every time, get the transformation from the static cache.
        //it will improve the load time of transformation object
        XslCompiledTransform xslt = PrepareTransformationObject(transformation.XSLTName);
        XsltArgumentList args = new XsltArgumentList();
        //adding extension object, it will work as a helper method from XSLT
        args.AddExtensionObject(“urn:Util”, new Util(transformation.XsltParameters));
        StringWriter stringWriter = new StringWriter();
        XmlTextWriter writer = new XmlTextWriter(stringWriter);
        //applying tranform
        xslt.Transform(reader, args, writer);
        //set the output xml
        transformation.DestinationXML = stringWriter.ToString();
    }
}

This method stores the transformation object in static cache, so next time if you are using the same XSLT it will retrieve the object from cache instead of loading it again.

static Hashtable _xsltText = new Hashtable();
/// <summary>
/// Return the tranformation object, either create new or get from static cache if already available
/// </summary>
/// <param name=”transformationName”>full path of xslt</param>
/// <returns></returns>
static XslCompiledTransform PrepareTransformationObject(string transformationName)
{
    if (_xsltText.ContainsKey(transformationName))
        return (XslCompiledTransform)_xsltText[transformationName];
    else
    {
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(transformationName);
        if (!_xsltText.ContainsKey(transformationName))
        {
            //you can lock the object if you face the concurrency issues
            //lock (lockObj)
            //{
            _xsltText.Add(transformationName, xslt);
            //}
        }
        return xslt;
    }
}

Example using the above methods:

string xsltName = Server.MapPath(@”XSLT\Sample.xslt”);
string inputXML = XElement.Load(Server.MapPath(@”XML\input.xml”)).ToString();
string outputXML = string.Empty;
//passing the current date as transformation date, you can pass any object and then access it in util class.
XsltParam xsltParam = new XsltParam();
xsltParam.Params.Add(“TransactionDate”, DateTime.Now.ToShortDateString());
//prepare the tranformation container
TransformationContainer container = new TransformationContainer(xsltName, inputXML, outputXML, xsltParam);
TransformationHelper.Transform(container);
outputXML = container.DestinationXML;
XElement.Parse(outputXML).Save(Server.MapPath(@”XML\output.xml”));
Response.Write(outputXML);

Download the complete source XML-XSLT-TransformationSample.zip

Cheers!!


Tagged: asp.net, C#, compiledtransform, transform, XML, XSLT, XSLT transformation

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shabbir Lathsaheb
Software Developer (Senior)
India India
I am Shabbir Husain Lathsaheb (@SHLathsaheb). Programming is both my passion and profession. I work with a Web Application Development firm as a Sr. Software Engineer and in my free time I am a consultant, I love to sleep and am a movie buff. Do visit My blog to know more.
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 14 Apr 2014
Article Copyright 2014 by Shabbir Lathsaheb
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid