Scott Lock

Thoughts on .Net, Caparea.net and Windows Phone


News

My Stats

  • Posts - 77
  • Comments - 127
  • Trackbacks - 21

Tag Cloud


Recent Comments


Recent Posts


Archives


Post Categories


Image Galleries


Blogs I Read


Microsoft MVP


User Groups



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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

Gravatar # re: Transforming an XML String in Memory Outputing a String
Posted by Rob Foster on 3/21/2007 10:35 PM
Outstanding! That's a good one to keep handy.
Gravatar # Link Listing - March 21, 2007
Posted by Christopher Steen on 3/21/2007 11:22 PM
The WPF Book You Can't Live Without - WPF Unleashed [Via: brian@softinsight.com ] Don't just CodeBetter,...
Gravatar # re: Transforming an XML String in Memory Outputing a String
Posted by Roy on 6/11/2007 1:41 PM
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?


Gravatar # re: Transforming an XML String in Memory Outputing a String
Posted by Robin Thomas on 8/8/2008 5:49 AM
Thanx a lot...
Gravatar # re: Transforming an XML String in Memory Outputing a String
Posted by Balaji on 10/7/2011 2:25 PM
Thanks!!!!!!!! This helped me fix the issue.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: