Coder Perfect

Create an XDocument from a String


I’m working on a little project and trying to figure out if I can load an XDocument from a string. XDocument. The text given to Load() appears to be a path to a physical XML file.

I’d like to skip over the phase of creating the physical XML file and go right to populating the XDocument.

Any ideas?

Asked by StevenMcD

Solution #1

For this, you can use XDocument.Parse.

Answered by Ronald Wildenberg

Solution #2

XDocument is an option. Instead of Load, use Parse(string) (string).

Answered by Samuel

Solution #3

How about this…?

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

This was derived from the XDocument MSDN documentation. This is where you’ll find the load…

Answered by Martin Peck

Solution #4

Take a look at the Parse method.

Answered by bruno conde

Post is based on