Let's say I have two strings:
The xml and xsl data are stored in database columns, if you must know.
How can I transform the XML in C# w/o saving the xml and xsl as files first? I would like the output to be a string, too (HTML from the transformation).
It seems C# prefers to transform via files. I couldn't find a string-input overload for Load() in XslCompiledTransform. So, that's why I'm asking.
Java Code to Convert an XML Document to String For converting the XML Document to String, we will use TransformerFactory , Transformer and DOMSource classes. "</BookStore>"; //Call method to convert XML string content to XML Document object.
In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.
There are many methods you can use to transform XML documents including the XSLTRANSFORM function, an XQuery update expression, and XSLT processing by an external application server.
XSLT is commonly used to convert XML to HTML, but can also be used to transform XML documents that comply with one XML schema into documents that comply with another schema. XSLT can also be used to convert XML data into unrelated formats, like comma-delimited text or formatting languages such as troff.
Here's what I went with. It's a combination of your answers. I voted up the answers that inspired this:
string output = String.Empty; using (StringReader srt = new StringReader(xslInput)) // xslInput is a string that contains xsl using (StringReader sri = new StringReader(xmlInput)) // xmlInput is a string that contains xml { using (XmlReader xrt = XmlReader.Create(srt)) using (XmlReader xri = XmlReader.Create(sri)) { XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(xrt); using (StringWriter sw = new StringWriter()) using (XmlWriter xwo = XmlWriter.Create(sw, xslt.OutputSettings)) // use OutputSettings of xsl, so it can be output as HTML { xslt.Transform(xri, xwo); output = sw.ToString(); } } }
Note: this statement is required in the xsl, in order to output as HTML:
<xsl:output method="html" omit-xml-declaration="yes" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With