...Technology Simplified

Wednesday, July 11, 2012

XSLT transformation using Windows Service

No comments :

private string ArchivePath = @"c:\Archive\";
private string InputPath = @"c:\InputFiles\";
private string OutputPath = @"c:\OutputFiles\";
To write the exception entries to Event Viewer

protected override void OnStart(string[] args)
{
Transformation();
try
{
// Uncomment this line to debug...
//System.Diagnostics.Debugger.Break();

_thread = new System.Threading.Thread(Transformation);
_thread.Start();
EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);
}
catch (Exception ex)
{
// Log the exception.
EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
}


private void Transformation()
{
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
// Source paths
string xsltFilePath = "D:\\Transformation.xslt";
// refer link http://technovogeek.blogspot.ie/2012/07/how-to-display-current-date-and-time-in.html for using sample XSLT
// Compile the style sheet.
XsltSettings xslt_settings = new XsltSettings();
xslt_settings.EnableScript = true;
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsltFilePath, xslt_settings, new XmlUrlResolver());
try
{
string[] inputfileEntries = Directory.GetFiles(InputPath);
foreach (string fileName in inputfileEntries)
{
// Load the XML source file.
XPathDocument doc = new XPathDocument(fileName);

// Create an XmlWriter.
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

if (!System.IO.Directory.Exists(OutputPath))
System.IO.Directory.CreateDirectory(OutputPath);
string file = System.IO.Path.Combine(OutputPath, System.IO.Path.GetFileNameWithoutExtension(fileName) + "_OUTPUT.xml");

XmlWriter writer = XmlWriter.Create(file, settings);

if (!System.IO.Directory.Exists(ArchivePath))
System.IO.Directory.CreateDirectory(ArchivePath);
string destFile = System.IO.Path.Combine(ArchivePath, System.IO.Path.GetFileName(fileName));
System.IO.File.Copy(fileName, destFile, true);
// Execute the transformation.
xslt.Transform(doc, writer);
writer.Close();
System.IO.File.Delete(fileName);
}
}
catch (Exception ex)
{
string str = "Unable to process the Conversion ";
}
}

No comments :

Post a Comment