Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily get a TextReader from an XDocument?

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?


Edit

The above example is equivalent to converting the entire instance to an XML string and then create a TextReader over that string.

I was just wondering whether there's a more stream-like way to do it than reading the entire contents into memory.

like image 529
Mark Seemann Avatar asked Oct 15 '22 07:10

Mark Seemann


1 Answers

  TextReader tr = new StringReader(xml.ToString());
like image 82
Daniel Elliott Avatar answered Oct 18 '22 14:10

Daniel Elliott