Accueil > > > 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
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:\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
Commentaires et avis
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 ]
Bonjour,J'essaye de trouver des pistes pour inserer des donné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 à tous,J'ai un fichier xml dont j'ai appliqué un filtre xslt pour en extraire des donné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
XML + XSL --> export excel (pbm de type) [ par 76julien ]
Bonjour la communauté, je vous expose mon problème : j'ai un fichier XML associé à un fichier XSL pour le schéma. L'export se passe bien excepté une
[C #/XML] problemes avec xmlWriter [ par moha_yougo ]
hélloje veut céer le noyau d'une feuille de styles xslt:<?xml version="1.0"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.
[c#/ xml] comment faire modification d'un document xml ou xsl [ par moha_yougo ]
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="
|
Derniers Blogs
TECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYSTECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYS par ROMELARD Fabrice
Speakers : Lionel Limozin et Alain Marty La session commence par une découverte de SharePoint à travers la mise en place d'un environnement SharePoint pour la gestion des Sessions animées par BeWise. Le besoin est très ba...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice PERSPECTIVE 3.0 POUR SILVERLIGHT 5.0PERSPECTIVE 3.0 POUR SILVERLIGHT 5.0 par odewit
Je viens de publier la version 3.0 de Perspective pour Silverlight, qui regroupe un portage sous Silverlight 5.0 des fonctionnalités de Perspective 2.0, le framework 3D de haut-niveau introduit récemment et de nouveaux exemples de code. En voici la li...
Cliquez pour lire la suite de l'article par odewit TECHDAYS PARIS 2012 : TOP 10 DES BEST PRACTICES POUR SQL SERVERTECHDAYS PARIS 2012 : TOP 10 DES BEST PRACTICES POUR SQL SERVER par ROMELARD Fabrice
Speaker : Nadia Ben El Kadi Configuration machine La session commence par la toute première question à se poser lors de la mise en place d'environnement SQL Server, la configuration des machines : Type de mac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : KINECT + OFFICE 365 UN BON GESTE POUR VOTRE SITECHDAYS PARIS 2012 : KINECT + OFFICE 365 UN BON GESTE POUR VOTRE SI par ROMELARD Fabrice
Speakers : Fabrice Barbin, Samuel Blanchard, Julien Lo Presti Titre Prometteur et attractif invitant à voir comment lier le composant ludique Kinect dans le cadre d'une structure IT classique, notamment au travers de la plat...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE DU PREMIER JOURTECHDAYS PARIS 2012 : PLEINIèRE DU PREMIER JOUR par ROMELARD Fabrice
KeyNotes du premier jour pour les développeurs. La session est principalement axée sur une des principales directions prise par Microsoft à travers tous ses nouveaux produits : Cloud privé ou public (Solution Azure) ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|