I am trying to transform and XML document using XSL. I am not too familiar with how to transform XML in .NET so I am using some example code ...
XslCompiledTransform xslTransformer = new XslCompiledTransform();
xslTransformer.Load(Server.MapPath("Test.xslt"));
MemoryStream ms = new MemoryStream();
xslTransformer.Transform(Server.MapPath("Test.xml"), null, ms);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
string output = sr.ReadToEnd();
ms.Close();
Response.Write(output);
The problem I am running into is the following line ...
xslTransformer.Transform(Server.MapPath("Test.xml"), null, ms);
The compiler is showing an error in passing in null
to the Transform method. In looking at the Transform
method signatures it looks like the parameter null
is representing is various options for the transformation. The Visual Studio IDE even shows that this value can be null
. I also tried using DBNull.Value
but that also shows an error. Any idea what I am missing?
UPDATE: The error I am receiving is two-fold. (1) First I am told that null
is no longer supported and I should use System.DBNull
. (2) Once I change null
to System.DBNull
I get an error saying overload resolution failed because no accessible Transform can be called with these arguments.
You can try using a different XslCompiledTransform.Transform method overload, the one which accepts a String path and an XmlWriter:
Actually, if you are only interesting in rendering XML to a string, you can use a
StringBuilder
as the target, without aMemoryStream
, and simplify your code to:[Edit] (based on your comment)
If the input XML is in a string, then you can use yet another
XslCompiledTransform.Transform
method overload:Transform(XmlReader, XmlWriter)
.XmlReader
can be (among other ways) instantiated using a concrete implementation ofTextReader
, in this case aStringReader
. You can also use an other stream as input.When you put all that together, you should end up with:
All of these classes implement
IDisposable
, so you need to make sure they are disposed after use.Are you sure it's the middle argument that's causing the error? Perhaps
Server.MapPath("Test.xml")
is returning null?