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?
TextReader tr = new StringReader("<Root>Content</Root>"); XDocument doc = XDocument. Load(tr); Console. WriteLine(doc); This was taken from the MSDN docs for XDocument.
One possible use for this method is to create a copy of a DOM document in a LINQ to XML tree. To do this, you create an XmlNodeReader from a DOM document, and then use the XmlNodeReader to create an XDocument. LINQ to XML's loading functionality is built upon XmlReader.
XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.
Parse(String) Creates a new XDocument from a string. Parse(String, LoadOptions) Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.
You can use XDocument.Parse
for this.
You can use XDocument.Parse(string)
instead of Load(string)
.
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
Try the Parse method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With