begin process at 2008 08 22 04:17:11
1 229 768 membres
39 nouveaux aujourd'hui
14 267 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

XMLTRANSFORMER - CLASSE POUR SIMPLIFIER LA TRANSFORMATION EN UTILISANT DES FICHIERS XSL


Information sur la source

Catégorie :.NET Source .NET ( DotNet ) Classé sous : xslcompiledtransform, xslt, xsl, xml Niveau : Débutant Date de création : 10/07/2007 Date de mise à jour : 10/07/2007 13:38:24 Vu : 4 449

Note :
6 / 10 - par 1 personne
6,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

Description

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

  • 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);
  • }
  • }
  • }
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);
10 juillet 2007 13:38:24 :
Ajout d'un exemple
  • signaler à un administrateur
    Commentaire de titwan le 31/08/2007 15:41:36

    Pouvez vous preparer ceci sous forme de fichier a telecharger,

    merci

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS