using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Xsl;
using System.Xml.XPath;
namespace IDS.Rapport.Dell.Helpers
{
public class XmlHelper
{
#region Methods
//public static string ToXmlString(Object Input)
//{
// try
// {
// XmlWriterSettings settings = new XmlWriterSettings();
// settings.OmitXmlDeclaration = true;
// MemoryStream ms = new MemoryStream();
// XmlWriter writer = XmlWriter.Create(ms, settings);
// XmlSerializerNamespaces names = new XmlSerializerNamespaces();
// names.Add("", "");
// XmlSerializer cs = new XmlSerializer(Input.GetType());
// cs.Serialize(writer, Input, names);
// ms.Flush();
// ms.Seek(0, SeekOrigin.Begin);
// StreamReader sr = new StreamReader(ms);
// return sr.ReadToEnd();
// }
// catch (Exception ex) {
// throw new Exception("Exception occurred while serializing object to XML string.", ex);
// }
//}
public static string TransformInMemory(XslCompiledTransform Transform, XmlDocument InputXML)
{
StringBuilder sb = new StringBuilder();
try
{
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.ConformanceLevel = ConformanceLevel.Fragment;
XmlWriter xWriter = XmlWriter.Create(sb, wSettings);
Transform.Transform(new XmlNodeReader(InputXML), xWriter);
}
catch (Exception ex)
{
//Error Logging
ErrorHandler.WriteError(ex, "XmlHelper.cs TransformInMemory");
}
finally
{
GC.Collect();
}
return sb.ToString();
}
#endregion
}
}
Calling the functions
TransformInMemory
XmlDocument returnDoc = new XmlDocument();
string OrderImportXSLPath = HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["OrderImportXSLTPath_WithinRapport"]).Trim());
XslCompiledTransform xslTransform1 = new XslCompiledTransform();
xslTransform1.Load(OrderImportXSLPath, new XsltSettings(false, true), new XmlUrlResolver());
returnDoc.LoadXml(IDS.Rapport.Dell.Helpers.XmlHelper.TransformInMemory(xslTransform1, returnDoc));
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Xsl;
using System.Xml.XPath;
namespace IDS.Rapport.Dell.Helpers
{
public class XmlHelper
{
#region Methods
//public static string ToXmlString(Object Input)
//{
// try
// {
// XmlWriterSettings settings = new XmlWriterSettings();
// settings.OmitXmlDeclaration = true;
// MemoryStream ms = new MemoryStream();
// XmlWriter writer = XmlWriter.Create(ms, settings);
// XmlSerializerNamespaces names = new XmlSerializerNamespaces();
// names.Add("", "");
// XmlSerializer cs = new XmlSerializer(Input.GetType());
// cs.Serialize(writer, Input, names);
// ms.Flush();
// ms.Seek(0, SeekOrigin.Begin);
// StreamReader sr = new StreamReader(ms);
// return sr.ReadToEnd();
// }
// catch (Exception ex) {
// throw new Exception("Exception occurred while serializing object to XML string.", ex);
// }
//}
public static string TransformInMemory(XslCompiledTransform Transform, XmlDocument InputXML)
{
StringBuilder sb = new StringBuilder();
try
{
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.ConformanceLevel = ConformanceLevel.Fragment;
XmlWriter xWriter = XmlWriter.Create(sb, wSettings);
Transform.Transform(new XmlNodeReader(InputXML), xWriter);
}
catch (Exception ex)
{
//Error Logging
ErrorHandler.WriteError(ex, "XmlHelper.cs TransformInMemory");
}
finally
{
GC.Collect();
}
return sb.ToString();
}
#endregion
}
}
Calling the functions
TransformInMemory
XmlDocument returnDoc = new XmlDocument();
string OrderImportXSLPath = HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["OrderImportXSLTPath_WithinRapport"]).Trim());
XslCompiledTransform xslTransform1 = new XslCompiledTransform();
xslTransform1.Load(OrderImportXSLPath, new XsltSettings(false, true), new XmlUrlResolver());
returnDoc.LoadXml(IDS.Rapport.Dell.Helpers.XmlHelper.TransformInMemory(xslTransform1, returnDoc));
No comments:
Post a Comment