- 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);
}
}
}