Scott Lock's Blog

Building the .Net Community through Caparea.Net, Consulting, and no sleep
posts - 59, comments - 29, trackbacks - 22

My Links

News

Twitter












Archives

Post Categories

Blogs I Read

Microsoft MVP

User Groups

Transforming an XML String in Memory Outputing a String

If you ever wanted to take XML as string in memory, transform it, and out the results as a string:

private bool verifyTransformation(string xml, string xslPath)

{

//create an XPathDocument using the reader containing the XML

MemoryStream m = new MemoryStream(System.Text.Encoding.Default.GetBytes(xml));

XPathDocument xpathDoc = new XPathDocument(new StreamReader(m));

//Create the new transform object

XslCompiledTransform transform = new XslCompiledTransform();

//String to store the resulting transformed XML

StringBuilder resultString = new StringBuilder();

XmlWriter writer = XmlWriter.Create(resultString);

transform.Load(xslPath);

transform.Transform(xpathDoc,writer);

}

Now you can access the results by using the ToString() method of the StringBuilder.

Print | posted on Wednesday, March 21, 2007 10:06 AM |


Feedback

# re: Transforming an XML String in Memory Outputing a String

Outstanding! That's a good one to keep handy. 3/21/2007 10:35 PM | Rob Foster

# Link Listing - March 21, 2007

The WPF Book You Can't Live Without - WPF Unleashed [Via: brian@softinsight.com ] Don't just CodeBetter,... 3/21/2007 11:22 PM | Christopher Steen

# re: Transforming an XML String in Memory Outputing a String

Is there a flag to set after or before the transformation to say the transformation output is going to be plain text and not an xml?

6/11/2007 1:41 PM | Roy

# re: Transforming an XML String in Memory Outputing a String

Thanx a lot... 8/8/2008 5:49 AM | Robin Thomas

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 1 and type the answer here:

Powered by: