Populate XDocument from String

C#XmlC# 3.0Linq to-Xml

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

You can use XDocument.Parse for this.

Solution 2 - C#

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

Solution 3 - C#

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

Solution 4 - C#

Try the Parse method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStevenMcDView Question on Stackoverflow
Solution 1 - C#Ronald WildenbergView Answer on Stackoverflow
Solution 2 - C#SamuelView Answer on Stackoverflow
Solution 3 - C#Martin PeckView Answer on Stackoverflow
Solution 4 - C#bruno condeView Answer on Stackoverflow