Accessing Data with Microsoft .NET Framework 4: LINQ to XML
- By Glenn Johnson
XML has been a rapidly growing technology because it provides a verbose means for transferring data that can be understood easily by computers as well as by people. You will often need to query the XML data.
Another common requirement is to transform XML into a different format. In some scenarios, you simply want to convert XML to a different form of XML. In other scenarios, you might want to convert XML into HTML. You might even want to convert XML into text.
This chapter’s first lesson shows how you can use XmlDocument and XmlReader classes to query XML data. Lesson 2, “Querying with LINQ to XML”, shows how you can use LINQ to XML to retrieve data from XML. Lesson 3, “Transforming XML Using LINQ to XML”, uses LINQ to XML to transform XML data.
Before You Begin
You must have some understanding of Microsoft C# or Visual Basic 2010, and you should be familiar with XPath query language, also known as XML Path Language. XPath is an XML technology that was created to provide a common syntax and semantics to address parts of an XML document. XPath has been a W3C (World Wide Web Consortium, http://www.w3c.org) recommendation since November 1999.
XPath uses a path notation for navigating through the hierarchical structure of an XML document that is similar to that used for navigating the folder hierarchy on your disk drive when you locate a file. Just as you can locate a file by specifying a relative or explicit path, you can locate parts of an XML document by supplying a relative or explicit path. Even the asterisk (*) is useful as the “all” wildcard when locating parts of an XML document. This chapter exposes you to simple XPath queries, but XPath is not the chapter’s focus.
This chapter requires only the hardware and software listed at the beginning of this book.