This is really a question about Stream
and TextWriter
, not XML. All of the documentation for .NET's XSLT tools assumes you already know how to do stream- and text-based IO in .NET. If you don't, you may end up doing a lot of unnecessary work. (By "you" I mean "I", and by "may" I mean "did.")
Though Martin Honnen correctly observes that in your case you don't actually need to get your output to a string, here's how to do it. Note that this works with any method that writes to a TextWriter
, not just XslCompiledTransform.Transform()
:
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
MethodThatWritesToATextWriter(sw);
}
string s = sb.ToString();
Though you can use a FileStream
to write to a file in your specific case, FileStream
is a Stream
, not a TextWriter
. You can only use it with Transform
because one of the overloads of Transform
takes a Stream
as the output parameter. If you need to use a TextWriter
, the tool for the job is StreamWriter
:
using (StreamWriter sw = new StreamWriter(filename, append))
{
MethodThatWritesToATextWriter(sw);
}