383

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?

Otiel
  • 18,404
  • 16
  • 78
  • 126
StevenMcD
  • 17,262
  • 11
  • 42
  • 54

4 Answers4

584

You can use XDocument.Parse for this.

Ronald Wildenberg
  • 31,634
  • 14
  • 90
  • 133
84

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

Samuel
  • 37,778
  • 11
  • 85
  • 87
43

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
  • 11,440
  • 2
  • 42
  • 69
24

Try the Parse method.

bruno conde
  • 47,767
  • 15
  • 98
  • 117