Reading XML documents using LINQ to XML

Source: Internet
Author: User
Tags file system xpath visual studio

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.