How can I transform XML to HTML with XSLT in ASP.NET Core?
I thought about:
public static string TransformXMLToHTML(string inputXml, string xsltString)
{
XslCompiledTransform transform = new XslCompiledTransform();
using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) {
transform.Load(reader);
}
StringWriter results = new StringWriter();
using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) {
transform.Transform(reader, null, results);
}
return results.ToString();
}
but the XmlReader does not exist in .NET Core.
Do you have any idea?
The
System.Xml.Xsl
disappeared in.NET Core 1.0
asXSD (XmlSchema)
orXSLT (XslTransform)
is not supported in.NET Standard 1.0
, which.NET Core
implements until version '.NET Core 2.0'. Good news are that since.NET Core 2.0
it implements.NET Standard 2.0
, so we haveSystem.Xml.Xsl
again.So to answer your question, you need to upgrade your
.NET Core
app to.NET Core 2.0
and your code will work again.If you need to return
XDocument
you can the code below, which is similar to your but return instance of 'XML' document: