Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XLinq Part 2: Using Standard Query Operators

XLinq's query power goes way beyond the now-common methods of retrieving XML and querying it with XPath.


advertisement
n Part 1 of this article series, you looked at XLinq's ability to create, modify, and delete XML data. Building on that background, this installment focuses on XLinq's query features, by first delving into the standard query operators supplied with LINQ and then providing examples of using them to query XML data. Finally this article will also provide an example of transforming XML data to a collection using the new LINQ query format.

Querying XML
The main goal of the LINQ family of technologies is to add general purpose query facilities to the .NET Framework so that you can query all sources of information (including the relational and XML data) through a unified query approach. Because of the generic query foundation, you can apply LINQ to query any class as long as that class implements IEnumerable<T> class. This also means that you can potentially leverage LINQ from any .NET language (it's currently being added to C# and VB.NET) because the underlying LINQ API is nothing but a set of .NET classes.

XLinq is an XML query language that inherits from the LINQ query foundation. You can use it to query XLinq objects such as XElement, XDocument, etc using LINQ query facilities.

Performing a Simple Query
Here's a simple XML file called Products.xml (available with the downloadable sample code) that I've used as the source document for the query capabilities described in this article:

<?xml version="1.0" encoding="utf-8" ?> <Products> <Product ID="1"> <Name>Adjustable Race</Name> <Number>AR-5381</Number> </Product> <Product ID="2"> <Name>Bearing Ball</Name> <Number>BA-8327</Number> </Product> <Product ID="3"> <Name>BB Ball Bearing</Name> <Number>BE-2349</Number> </Product> <Product ID="317"> <Name>LL Crankarm</Name> <Number>CA-5965</Number> </Product> Number="" /> <Product ID="318"> <Name>ML Crankarm</Name> <Number>CA-6738</Number> </Product> <Product ID="318"> <Name>ML Crankarm</Name> <Number>CA-6738</Number> </Product> </Products>


The Products.xml file is a simple product list where each product consists of an id, name, and number. To query this file with XLinq, first create an ASP.NET page named SimpleXLinqQuery.aspx, and then set the page's contents as shown below:

<%@ Page Language="C#" %> <%@ Import Namespace="System.Data.DLinq" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Xml.XLinq" %> <%@ Import Namespace="System.Expressions" %> <%@ Import Namespace="System.Query" %> <%@ Import Namespace="System.Web.Configuration" %> <script runat="server"> void btnAllGetProductNames_Click (object sender, EventArgs e) { XElement products = XElement.Load(Server.MapPath ("~/App_Data/Products.xml")); var productNames = from prod in products.Elements("Product") orderby (string) prod.Element("Name") select (string) prod.Element("Name"); gridResult.DataSource = productNames; gridResult.DataBind(); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Simple XLinq Query</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button runat="Server" Text="Get All Product Names" ID="btnAllGetProductNames" OnClick="btnAllGetProductNames_Click"/> <br /><br/> <asp:GridView runat="server" ID="gridResult" /> </div> </form> </body> </html>


At the top of the page, you can see import statements for all the required namespaces. One key namespace is System.Query, which contains a number of powerful classes that provide native querying capabilities. This example shows how to return all the product names in the Products.xml file.

Add a button control and a GridView control to your ASP.NET page. In the Click event code for the button, start by loading the XML file into memory using the Load() method of the XElement class.



XElement products = XElement.Load(Server.MapPath ("~/App_Data/Products.xml"));


After loading the XML file, the next step is to retrieve all the product names.

var productNames = from prod in products.Elements("Product") orderby (string) prod.Element("Name") select (string) prod.Element("Name");


The preceding line of code does the majority of the work. Here's a more detailed examination.

 
Figure 1. Product Names Output: Here's the output of the SimpleXLinqQuery.aspx example, showing the list of product names retrieved from the Products.xml file.
The var keyword signals the compiler that you are using the new local variable Type Inference feature in .NET Framework 3.0. This var keyword differs from the JavaScript var keyword (which creates a variable capable of holding any type of data) in that it tells the C# compiler to infer the type from the assignment of values. As an example, if you assign "1" to a variable of type var, the compiler will infer the type of that variable to be int

On the right side of the equals sign, the code uses a from..select syntax to query the XML file. This statement selects all <Name> elements from the <Product> elements within the Products.xml file and returns them as an enumerable String collection of product names. The orderby clause orders the items within the collection by the contents of the Name element, which is typecast into a string before the results are ordered.

Once you have the list of product names, you can easily bind it to a GridView by setting the DataSource property (see Figure 1).

gridResult.DataSource = productNames; gridResult.DataBind();




Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap