Language-level integrated queries for XML (LINQ to XML) allow the use of standard query operators to query XML data just like a tree operator, which provides navigation like XPath to descendants, ancestors, and sibling XML elements. It simplifies the use of XML data without having to use additional language syntax like XPath or XQuery. You can use LINQ to XML to perform LINQ queries on XML that you obtain from a file system, from a remote Web service, or from an in-memory XML content. This article will focus on using LINQ to query XML from an XML file--customers.xml file.
To create a simple LINQ project
We will name our Visual Studio project as Consolelinqtoxml and use the C # language. Add the following namespace with the using directive. The System.Xml.Linq namespace contains classes that are used by LINQ to XML.
//Include the LINQ to XML namespaces
using System.Xml; //namespace to deal with XML documents
using System.Xml.Linq; //namespace to deal with LINQ to XML classes
Next, we'll add the Customers.xml file to our project. You can also choose to store the XML file in a directory on your filesystem, but for simplicity we will only include it in our project. Click the Project menu in Visual Studio and select Add Existing Item ....
Figure 1
Select the Customers.xml file and click Add. This XML file will be added to your project.
Figure 2
Right-click the file Customers.xml from Solutions Explorer and select Properties. This displays the Properties window.
Figure 3