How to transform XML as a string w/o using files in .NET?
Let's say I have two strings:
- one is XML data
- and the other is XSL data.
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).
开发者_如何学GoIt 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.
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" />
You can XmlReader.Create() from a StringReader or a MemoryStream . XslCompileTransfrom can Load() from an XmlReader.
It took me a long time (literally years) to work out how concise code using Stream
and/or TextWriter
can be if you use the proper idioms.
Assuming transform
and input
are strings:
StringWriter sw = new StringWriter();
using (XmlReader xrt = XmlReader.Create(new StringReader(transform))
using (XmlReader xri = XmlReader.Create(new StringReader(input))
using (XmlWriter xwo = XmlWriter.Create(sw))
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xrt);
xslt.Transform(xri, xwo);
}
string output = sw.ToString();
I send the xml content and then load the XSLT document, apply the transformation and then return the new xml.
public static string Transform(string xmlString)
{
string output = String.Empty;
try
{
// Load an XML string into the XPathDocument.
StringReader rdr = new StringReader(xmlString);
XPathDocument myXPathDoc = new XPathDocument(rdr);
var myXslTrans = new XslTransform();
//load the Xsl
myXslTrans.Load("XSLTFile.xslt");
//create the output stream
StringWriter sw = new StringWriter();
XmlWriter xwo = XmlWriter.Create(sw);
//do the actual transform of Xml
myXslTrans.Transform(myXPathDoc, null, xwo);
output = sw.ToString();
xwo.Close();
return output;
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
throw;
}
}
NOTE: "XSLTFile.xslt" It is added to the solution and set the property "Copy to Output Directory" to "Copy always".
edit: using-blocks added
// input-xml
string xmlinput = String.Empty;
// xslt
string xsltinput = String.Empty;
// output-xml
string xmloutput = String.Empty;
// Prepare input-xml
XPathDocument doc = new XPathDocument(new StringReader(xmlinput));
// Prepare XSLT
XslTransform xslt = new XslTransform();
// Creates a XmlReader from your xsl string
using (XmlReader xmlreader = XmlReader.Create(new StringReader(xsltinput)))
{
//Load the stylesheet.
xslt.Load(xmlreader);
// transform
using (StringWriter sw = new StringWriter())
{
xslt.Transform(doc, null, sw);
// save to string
xmloutput = sw.ToString();
}
}
A VB.Net version inspired by Robert Rossney's answer:
Private Function TransformXML(XMLPath As String, XSLPath As String) As String
Dim XSLT As XslCompiledTransform = New XslCompiledTransform()
Dim sWriter As StringWriter = New StringWriter
Dim xReader As XmlReader = XmlReader.Create(XMLPath)
Using xWriter As XmlWriter = XmlWriter.Create(sWriter)
XSLT.Load(XSLPath)
XSLT.Transform(xReader, xWriter)
End Using
Return sWriter.ToString
End Function
I would use the XmlReader.Create(DatabaseBlobStream)
and XmlWriter.Create(StringBuilder)
overloads. Using the following DatabaseBlobStream object
DatabaseBlobStream.cs
internal class DatabaseBlobStream : Stream
{
private readonly IDataReader reader;
private readonly int columnIndex;
private long streamPosition;
internal DatabaseBlobStream(IDataReader reader, int columnIndex)
{
this.reader = reader;
this.columnIndex = columnIndex;
}
public override bool CanRead
{
get
{
return reader.GetFieldType(columnIndex) == typeof (byte[])
&& !reader.IsDBNull(columnIndex);
}
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush()
{
throw new Exception("This stream does not support writing.");
}
public override long Length
{
get { throw new Exception("This stream does not support the Length property."); }
}
public override long Position
{
get
{
return streamPosition;
}
set
{
streamPosition = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
if (reader.IsDBNull(columnIndex))
return 0;
int bytesRead = (int)reader.GetBytes(columnIndex, streamPosition + offset, buffer, 0, count);
streamPosition += bytesRead;
return bytesRead;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new Exception("This stream does not support seeking.");
}
public override void SetLength(long value)
{
throw new Exception("This stream does not support setting the Length.");
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new Exception("This stream does not support writing.");
}
public override void Close()
{
try
{
reader.Close();
}
finally
{
base.Close();
}
}
protected override void Dispose(bool disposing)
{
try
{
reader.Dispose();
}
finally
{
base.Dispose(disposing);
}
}
}
精彩评论