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
[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="
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
|
Derniers Blogs
TECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURSTECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURS par ROMELARD Fabrice
Animé par: Laurent Cotton Le développement dans SharePoint 2010 passe par plusieurs axes qui seront évoqués dans cette session, mais plus particulièrement les développements simples lié au besoin Business Business Connectivity Services Ce BCS es...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOURTECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOUR par ROMELARD Fabrice
Cette session est la dernière pleinière de ces 3 jours de TechDays Paris 2010. Généralement, cette troisième journée est plus axée sur l'avenir vu par Microsoft. Après un retour sur l'avenir vu par la Science Fiction ou par ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|