Skip to content

Instantly share code, notes, and snippets.

@ivanignatiev
Created December 20, 2015 21:41
Show Gist options
  • Save ivanignatiev/aa5f8686e325901b4dbf to your computer and use it in GitHub Desktop.
Save ivanignatiev/aa5f8686e325901b4dbf to your computer and use it in GitHub Desktop.
Apply XSLT to XML transformation on C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace ApplyXSLTToXML
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Error:");
Console.WriteLine("Not enough parameters to run this application");
return;
}
string XMLFilePath = args[0];
string XSLTFilePath = args[1];
// ApplyXSLTToXML.exe Input.xml Transformation.xslt
try
{
// Load documents
var xmlDocument = new XPathDocument(XMLFilePath);
var xslt = new XslCompiledTransform();
xslt.Load(XSLTFilePath);
// Apply transformation and output results to console
var consoleWriter = new StreamWriter(Console.OpenStandardOutput());
consoleWriter.AutoFlush = true;
xslt.Transform(xmlDocument, null, consoleWriter);
}
catch (Exception e)
{
Console.WriteLine("Error:");
Console.WriteLine(e.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment