using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.XPath; using System.IO; using System.Xml.Xsl; namespace Utilities { public class Xsl : IDisposable { //private variables private string mErrorDescription = string.Empty; //enums public enum XmlTypes : int { XmlData = 1, XmlFile = 2 } public string TransformXml(XmlTypes XmlType, string XmlSource, string XslFile) { return TransformXml(XmlType, XmlSource, new XslConfiguration(XslFile)); } public string TransformXml(XmlTypes XmlType, string XmlSource, XslConfiguration XslConfig) { //variables string ReturnValue = string.Empty; try { switch (XmlType) { case XmlTypes.XmlData: ReturnValue = TransformXmlPrivate(new XPathDocument(new StringReader(XmlSource)), XslConfig); break; case XmlTypes.XmlFile: ReturnValue = TransformXmlPrivate(new XPathDocument(XmlSource), XslConfig); break; } } catch { //Errors } //return return ReturnValue; } private string TransformXmlPrivate(XPathDocument XPathDoc, XslConfiguration XslConfig) { //variables XslCompiledTransform xXslCompiledTransform = new XslCompiledTransform(); StringWriter xStringWriter = new StringWriter(); XsltArgumentList xArgumentList = new XsltArgumentList(); string ReturnValue = string.Empty; try { //load xsl file xXslCompiledTransform.Load(XslConfig.XslFile); //parameters if (XslConfig.XslParameters != null) { foreach (KeyValuePair p in XslConfig.XslParameters) { xArgumentList.AddParam(p.Key, "", p.Value); } } //extension namespace if (XslConfig.ExtensionNameSpace.HasValue() && XslConfig.ExtensionObject != null) { xArgumentList.AddExtensionObject(XslConfig.ExtensionNameSpace, XslConfig.ExtensionObject); } //transform xXslCompiledTransform.Transform(XPathDoc, xArgumentList, xStringWriter); //return value ReturnValue = xStringWriter.ToString(); } catch (Exception ex) { mErrorDescription = ex.Message + " in Xsl:TransformXml"; } finally { xArgumentList = null; xStringWriter = null; xXslCompiledTransform = null; XPathDoc = null; } //return return ReturnValue; } //Properties public string ErrorDescription { get { return mErrorDescription; } } //Xsl Configuration Class public class XslConfiguration : IDisposable { //private variables private string mXslFile = string.Empty; private Dictionary mXslParameters = new Dictionary(); private string mExtensionNameSpace = string.Empty; private object mExtensionObject = null; public XslConfiguration() { //Nothing } public XslConfiguration(string XslFile) { mXslFile = XslFile; } public string XslFile { get { return mXslFile; } set { mXslFile = value; } } public Dictionary XslParameters { get { return mXslParameters; } } public void AddXslParameter(string Key, string Value) { mXslParameters.Add(Key, Value); } public string ExtensionNameSpace { get { return mExtensionNameSpace; } set { mExtensionNameSpace = value; } } public object ExtensionObject { get { return mExtensionObject; } set { mExtensionObject = value; } } #region "IDisposable Support" // To detect redundant calls private bool disposedValue; // IDisposable protected virtual void Dispose(bool disposing) { if (!this.disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects). } // TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. // TODO: set large fields to null. } this.disposedValue = true; } // TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources. //Protected Overrides Sub Finalize() // ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. // Dispose(False) // MyBase.Finalize() //End Sub // This code added by Visual Basic to correctly implement the disposable pattern. public void Dispose() { // Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(true); GC.SuppressFinalize(this); } #endregion } #region "IDisposable Support" // To detect redundant calls private bool disposedValue; // IDisposable protected virtual void Dispose(bool disposing) { if (!this.disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects). } // TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. // TODO: set large fields to null. } this.disposedValue = true; } // TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources. //Protected Overrides Sub Finalize() // ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. // Dispose(False) // MyBase.Finalize() //End Sub // This code added by Visual Basic to correctly implement the disposable pattern. public void Dispose() { // Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(true); GC.SuppressFinalize(this); } #endregion } }