Parsing, processing, and generating XML
Some apps, especially apps used with existing systems, need to be able to parse and generate XML.
Getting ready
For this recipe, we need to have an existing XML file stored in the Assets
folder. The structure of the XML file that we will use in this recipe is as follows:
<?xml version="1.0" encoding="UTF-8"?> <bookshelf> <book title="book title"> <authors> <author firstname="name" lastname="surname"/> <author ... /> </authors> </book> <book>...</book> </bookshelf>
How to do it...
There are many ways to handle XML, but in this recipe, we will use Language-Integrated Query (LINQ) to XML. One of the most common things to do with XML is to read it, usually from a remote source. Let's take a look at the following steps:
Ensure that the project includes the
System.Xml.Linq.dll
reference.In this instance, we will just read an asset and load it into an
XDocument
instance...