StevenMcD
StevenMcD

Reputation: 17482

Populate XDocument from String

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?

Upvotes: 391

Views: 157980

Answers (4)

Ronald Wildenberg
Ronald Wildenberg

Reputation: 32104

You can use XDocument.Parse for this.

Upvotes: 598

Martin Peck
Martin Peck

Reputation: 11554

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

Upvotes: 43

bruno conde
bruno conde

Reputation: 48265

Try the Parse method.

Upvotes: 24

Samuel
Samuel

Reputation: 38346

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

Upvotes: 87

Related Questions