Skip to content

Instantly share code, notes, and snippets.

@meziantou
Created December 10, 2018 02:23
Show Gist options
  • Save meziantou/2c377432b178ebab17a6802f189adce7 to your computer and use it in GitHub Desktop.
Save meziantou/2c377432b178ebab17a6802f189adce7 to your computer and use it in GitHub Desktop.
XSLT sample
static void Main(string[] args)
{
var xsl = XDocument.Parse(@"<xsl:stylesheet version=""1.0""
xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""
xmlns:msxsl=""urn:schemas-microsoft-com:xslt""
xmlns:user=""http://dummy/ns"">
<msxsl:script language=""C#"" implements-prefix=""user"">
<![CDATA[
public string Code()
{
return DateTime.Now.ToString();
}
]]>
</msxsl:script>
<xsl:template match=""/"">
<xsl:value-of select=""user:Code()""/>
</xsl:template>
</xsl:stylesheet>"
);
var myXslTrans = new XslCompiledTransform(enableDebug: true);
var settings = new XsltSettings();
settings.EnableScript = true;
myXslTrans.Load(xsl.CreateReader(), settings, null);
var doc = XDocument.Parse("<doc></doc>");
var reader = doc.CreateReader();
var sb = new StringBuilder();
var writer = XmlWriter.Create(sb, new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Fragment });
myXslTrans.Transform(reader, writer);
Console.WriteLine(sb.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment