Use hard coded xsl to convert xml document
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass
{
public static void Main()
{
string xslMarkup = @"<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:template match='/Parent'>
<Root>
<C1>
<xsl:value-of select='A1'/>
</C1>
</Root>
</xsl:template>
</xsl:stylesheet>";
XDocument xmlTree = new XDocument(
new XElement("Parent",
new XElement("A1", "A1 data"),
new XElement("A2", "A2 data")
)
);
XDocument newTree = new XDocument();
using (XmlWriter writer = newTree.CreateWriter())
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XmlReader.Create(new StringReader(xslMarkup)));
xslt.Transform(xmlTree.CreateReader(), writer);
}
Console.WriteLine(newTree);
}
}
Related examples in the same category