How can I easily get a TextReader from an XDocument?

Posted by Mark Seemann on Stack Overflow See other posts from Stack Overflow or by Mark Seemann
Published on 2010-04-09T14:33:33Z Indexed on 2010/04/09 14:43 UTC
Read the original article Hit count: 356

Filed under:
|
|

Given an XDocument instance, how can I easily get a TextReader that represents that instance?

The best I've been able to come up with is something like this (where xml is an XDocument instance):

var s = new MemoryStream();
var sw = new StreamWriter(s);

xml.Save(sw);

sw.Flush();
s.Position = 0;

TextReader tr = new StreamReader(s);

However, this seems a little clunky, so I was wondering if there's an easier way?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about xdocument