From the course: Complete Guide to .NET LINQ: Querying Collections, Databases, and Markup

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Parse XML from a string

Parse XML from a string

- [Instructor] There will be situations where XML data is not stored in a file, but instead exists as a string within your application. In these cases, you'll want to use the Parse method to load the XML. That's what we're looking at on line three. So let's take a look at the signature of this method. It takes a string and returns an XElement. So the string that I'm passing in starts on line 4 and ends on line 17. I've defined a multi-line string that includes XML elements. After calling Parse, the XML is loaded into the XElement instance, and then when I call Dump down here, you'll see that I'm getting the results. This output looks similar to what we observed earlier when loading XML from a file. In this case, the root element of the parsed XML is Card, which matches what we have in the string. Then you can see I have these sub-elements in the output. A key takeaway is that Parse provides a convenient way to load and work with XML that is already in memory. This is especially useful…

Contents