views:

5670

answers:

6

I'm working on a little something and I am trying to figure out whether I can load an XDocument from a string. XDocument.Load() seems to take the string passed to it as a path to a physical XML file.

I want to try and bypass the step of first having to create the physical XML file and jump straight to populating the XDocument.

Any ideas?

+33  A: 

You can use XDocument.Parse for this.

Ronald Wildenberg
thanks a million! Can't believe I overlooked it!
FailBoy
+7  A: 

You can use XDocument.Parse(string) instead of Load(string).

Samuel
+3  A: 

Try the Parse method.

bruno conde
+5  A: 

How about this...?

TextReader tr = new StringReader("<Root>Content</Root>");
XDocument doc = XDocument.Load(tr);
Console.WriteLine(doc);

This was taken from the MSDN docs for XDocument.Load, found here...

http://msdn.microsoft.com/en-us/library/bb299692.aspx

Martin Peck
But, as pointed out in other answers, Parse is the way to do this.
Martin Peck
Actually, Parse internally uses a StringReader.
Samuel
A: 

this is exactly what i was looking for thanks

Add a comment instead of a new answer to this question
Francis B.
A: 

yeah thanks.. i was also looking for this..

samimax