How? Writing the results of a XSL transformation to a string rather than to File

G

Guest

I am a bit of a newbie to C# and would like help with the following

I am bringing in a dataset and transforming that dataset using an XSL style sheet to generate text. This text does in fact contains SQL DDL statements (DBCC commands etc) which I want to capture and run on a SQL Server instance. At the moment I can do this by writing the result of the transformation to file and re-loading it, but would like to skip out the file and write directly into a string. How do I acheive this

Currently my code looks like this

{load DataSet xmltestDS with relational data

XmlDataDocument xmlDoc = new XmlDataDocument(xmltestDS);
XslTransform xslTran = new XslTransform()
xslTran.Load("C:\\XSLTTest.xsl");
XmlTextWriter writer = new XmlTextWriter("C:\\XSLTTest.txt", System.Text.Encoding.UTF8)
xslTran.Transform(xmlDoc, null, writer, null)

Thank

Stuart
 
G

Glen Jones MCSD

Stuart,

If I understand what your asking, you want to know how to Transform the
xmlDoc into a string.

Since XslTransform.Transform() method is overloaded and supports an stream
output param.
You could write to a stream, then Read() the stream to a byte[] buffer then
use BitConverter.ToString to convert it.

I'm sure there is another way, this is just my first thought.

Hope this helps.

--
Glen Jones MCSD

Stuart said:
I am a bit of a newbie to C# and would like help with the following.

I am bringing in a dataset and transforming that dataset using an XSL
style sheet to generate text. This text does in fact contains SQL DDL
statements (DBCC commands etc) which I want to capture and run on a SQL
Server instance. At the moment I can do this by writing the result of the
transformation to file and re-loading it, but would like to skip out the
file and write directly into a string. How do I acheive this?
 
C

Chad Evans

Stuart-

As far as I understand what you want to do, that is not write the Xslt
to a file and then have to load it from that file. You can accomplish
this by creating a StringReader() passing in the Xslt as a string in the
constructor, and then creating a XmlTextReader from the StringReader.
For example:

TextReader xsltTextReader = new StringReader(xsltData); // string
XmlReader xsltReader = new XmlTextReader(xsltTextReader);
XslTransform myXSLTrans = new XslTransform();
myXSLTrans.Load(xsltReader, null, null);
TextWriter htmlDoc = new StringWriter();
myXSLTrans.Transform(xmlDoc, null, htmlDoc, null);

Hope that helps.

Chad Evans
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top