Xmltransformer - classe pour simplifier la transformation en utilisant des fichiers xsl

Contenu du snippet

Voici une simple classe pour simplifier l'utilisation de la classe XslCompiledTransform qui permet de transformer un fichier XML à l'aide de fichier XSL.

Source / Exemple :


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;

namespace XMLTransform
{

    class XMLTransformer
    {

        private XslCompiledTransform _xslTransformer;

        public XMLTransformer(XmlDocument Source, XslCompiledTransform XSLTransformer)
        {
            _xslTransformer = XSLTransformer;
        }

        public string Transform(XmlDocument Source)
        {
            XPathDocument xpathDoc = new XPathDocument(new XmlNodeReader(Source));
            StringWriter sw = new StringWriter(new StringBuilder());

            _xslTransformer.Transform(xpathDoc, null, sw);

            return sw.ToString();
        }

        /// <summary>
        /// Direct transformation using XSL
        /// </summary>
        /// <param name="Source">XmlDocument as  Source file</param>
        /// <param name="XSLTransformer">XslCompiledTransform class to transform the XML source</param>
        /// <returns>Transformed XML File</returns>
        public static string DirectTransform(XmlDocument Source, XslCompiledTransform XSLTransformer)
        {
            XPathDocument xpathDoc = new XPathDocument(new XmlNodeReader(Source));
            StringWriter sw = new StringWriter(new StringBuilder());

            XSLTransformer.Transform(xpathDoc, null, sw);

            return sw.ToString();
        }

        /// <summary>
        /// Direct transformation using XSL
        /// </summary>
        /// <param name="Source">XmlDocument as  Source file</param>
        /// <param name="XSLFileName">XSLFile name to transform the XML source</param>
        /// <returns>Transformed XML File</returns>
        public static string DirectTransform(XmlDocument Source, string XSLFileName)
        {
            XslCompiledTransform _xslTransformer = new XslCompiledTransform();

            if (File.Exists(XSLFileName))
            {
                _xslTransformer.Load(XSLFileName);
                return DirectTransform(Source, _xslTransformer);
            }
            else
            {
                throw new FileNotFoundException(string.Format("Unable to find the XSL file : {0}", XSLFileName));
            }

        }

        /// <summary>
        /// Direct transformation using XSL
        /// </summary>
        /// <param name="SourceFile">XML filename as source</param>
        /// <param name="XSLFileName">XSL filename</param>
        /// <returns>Transformed XML File</returns>
        public static string DirectTransform(string SourceFile, string XSLFileName)
        {
            XmlDocument _source = new XmlDocument();
            XmlTextReader _xtr;

            if (File.Exists(SourceFile))
            {
                _xtr = new XmlTextReader(SourceFile);
            }
            else
            {
                throw new FileNotFoundException(string.Format("Unable to find the XSL file : {0}", SourceFile));
            }

            _source.Load(_xtr); // .LoadXml(SourceFile);

            return DirectTransform(_source, XSLFileName);
        }
    }
}

Conclusion :


Petit exemple :

// Retourne une string contenant le résultat de la transformation -> Méthodes statique
string _result;
_result = XMLTransform.XMLTransformer.DirectTransform(@"C:\Privat\devtest\XMLTransform\XMLTransform\result2.xml", @"C:\Privat\devtest\XMLTransform\XMLTransform\faqlight.xslt");

//
string _result2
XMLTransform.XMLTransformer _xmlTrans = new XMLTransform.XMLTransformer(@"C:\Privat\devtest\XMLTransform\XMLTransform\faqlight.xslt");
XMLDocument _xDocSource = new XMLDocument();

_xDocSource.Load(new System.Xml.XmlTextReader(@"C:\Privat\devtest\XMLTransform\XMLTransform\result2.xml"));
_result2 = _xmlTrans.Transform(_xDocSource);

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.