begin process at 2010 02 10 13:18:18
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

.NET

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

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


 Information sur la source

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

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
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 :6 278

Auteur : RayBan

Ecrire un message privé
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
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:\P rivat\devtest\XMLTransform\XMLTransform\result2.xm l", @"C:\Privat\devtest\XMLTransform\XMLTransform\faql ight.xslt");

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

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


 Historique

10 juillet 2007 13:38:24 :
Ajout d'un exemple

 Sources de la même categorie

Source avec Zip CHAT SERVER-CLIENT par abderrahmenbilog
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMULATION DE CONSOLE POUR WINDOWS MOBILE par originalcompo
Source avec Zip Source .NET (Dotnet) BASE DE DONNÉES EN XML par DanMor498
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMPLECONV - APPLICATION DE CONVERSION MONÉTAIRE AVEC TAUX E... par Jeffrey_
Source avec Zip Source .NET (Dotnet) TRAITEUR D'IMAGE (MINI) par ycyril

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Source .NET (Dotnet) [.NET3.5] SYSTEM.IO.PIPES - UTILISATION D'UN CANAL NOMMÉ par Willi
Source .NET (Dotnet) LINQ TO XML & LA MANIPULATION DE DONNÉES EN XML AVEC UNE APP... par driver
Source avec Zip Source avec une capture Source .NET (Dotnet) TRANSFORMATION D'UN XML À L'AIDE DE XSLT VERS HTML par EMSIEN
Source avec Zip Source .NET (Dotnet) PASSAGE DE PARAMÈTRES À UNE TRANSFORMATION XSLT par jesusonline
Source .NET (Dotnet) TRANSFORMATION XSLT par kbumbazz

Commentaires et avis

Commentaire de titwan le 31/08/2007 15:41:36

Pouvez vous preparer ceci sous forme de fichier a telecharger,

merci

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

XML [ par maevacmoi ] Hello !J'ai une fonction qui fait la transformation XSL en recevant un XML. La fonction ci-dessous fonctionne très bien si elle reçoit en paramètre : Inserer données venant d'une form dans fichiers XSL/XML [ par Obcts ] &nbsp;&nbsp;&nbsp;Bonjour,J'essaye de trouver des pistes pour inserer des donn&#233;es provenant de formulaires d'une forms dans un fichier XSL.Par ex XML, XSL et Date [ par creanova2000 ] Bonjour ,J'essaye de faire une xml transormation en utilisant un fichier xsl Tout Fonctionne sauf une chose qui n'est pas bien C'est que les date sont Transformation xml par xslt sur CF [ par nonold ] Bonjour,je cherche depuis pas mal de jours deja une solution pour pouvoir faire une transformation d'un xml via xslt pour en faire une page web. Sur l problème xml et xslt [ par c_ensias ] Bonjour &#224; tous,J'ai un fichier xml dont j'ai appliqu&#233; un filtre xslt pour en extraire des donn&#233;es.Je voudrais que la sortie soit un fic Différence entre XSL DOM ET LINQ [ par mimosa803 ] Bonjour,J'ai une question concernant l'utilisation des fichiers XML :Avant la sortie de LINQ TO XML on utilisait DOM pour parser le document XML (XMLD [C #/XML] problemes avec xmlWriter [ par moha_yougo ] h&#233;lloje veut c&#233;er le noyau d'une feuille de styles xslt:&lt;?xml version="1.0"?&gt;&lt;xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3. [c#/ xml] comment faire modification d'un document xml ou xsl [ par moha_yougo ] &nbsp;&nbsp;&nbsp; pour faire des modification sur un document xml ou xsl apartir d'un controle est ce que je doit copier ces fichies dans des fichier tableau HTML à partir de XSL [ par Licninoi ] Bonjour à tous, Je suis débutant en XML/XSL et je chercherais un moyen de créer un tableau à partir du XML suivant: [code=xml] <Tableur xmlns:xsi=" XML & XSL => Enregistrer résultat [ par maevacmoi ] Hello !J'ai un gros problème (C#.net). J'ai un fichier XML lié à un fichier XSL (~feuille de style). Le résultat s'affiche correctement dans le browse


Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,530 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales