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 1: Classes and Methods

XLinq, one of a set of extensions to .NET languages, improves on the XML DOM in both flexibility and expressive power. This article series shows you how to take advantage of XLinq in your own applications.


advertisement
early every .NET application needs to handle data that may reside in a wide range of data sources, from relational databases, to XML, to in-memory or disk-persisted objects. Although .NET Framework 2.0 exposes a number of classes to handle these types of data, it lacks a unified approach to handling data from disparate data sources. In other words, until now, you needed to learn a multitude of technologies such as ADO.NET, SQL, XML DOM, XML serialization, etc., to be able to read and update these various data sources. To solve this problem, Microsoft has released a set of technologies called Language-integrated Query (LINQ) that is specifically geared towards making data access consistent—regardless of the back-end data store. Key Components of LINQ
At this early stage, the LINQ family consists of three technologies, although Microsoft or third-party developers may add more in the future:

  • LINQ is a general purpose API that provides the core foundation for a consistent query experience.
  • DLinq (also known as LINQ to SQL) is a LINQ extension geared mainly towards relational databases.
  • XLinq (also known as LINQ to XML) is a LINQ extension for querying XML documents, as well as creating or transforming XML.
This article focuses primarily on XLinq, which provides an in-memory XML programming API for handling and processing XML data.
What You Need
Visual Studio 2005 Professional RTM and the LINQ May 2006 Community Technology Preview.

XLinq takes advantage of the standard query operators (where, select, order by, group by) supplied by LINQ and adds query extensions specific to XML. XLinq combines the query and transformation power of XQuery and XPath with the .NET Framework to provide a consistent query experience. Compared to the XML DOM (the traditional method of querying XML documents), the XLinq is a newly designed, lightweight API that provides significant improvements.

 
Figure 1. XLinq Class Hierarchy: The figure shows the classes and relationships in the System.Xml.XLinq namespace.
XLinq Class Hierarchy
You'll find the XLinq-related classes in the System.Xml.XLinq namespace as shown in Figure 1. Of the classes shown in Figure 1, the XNode and XContainer classes (shown in different color) are abstract. The XNode class is the base for element nodes, and provides a Parent method and methods such as AddBeforeThis, AddAfterThis, and Remove for updates in the imperative style. For IO, it provides methods for reading (ReadFrom) and writing (WriteTo).


Although the XElement class is bottom-most in the class hierarchy, it is the fundamental class. As the name suggests, it represents an XML element and allows you to perform the following operations:

  • Create elements with a specified element name
  • Change the element's contents
  • Add, change, or delete child elements
  • Add attributes to the element
  • Save the element as an XML fragment
  • Extract the contents in text form
Compared to the XML DOM, XLinq is much more flexible because it does not require you to always have a document object to be able to work with XML. Therefore, you can work directly with nodes and modify them as content of the document without having to start from a root XmlDocument object. This is a very powerful and flexible feature that you can use to compose larger trees and XML documents from tree fragments. Now that you have an overview of the XLinq's capabilities, the next few sections will examine the reading and writing features of XLinq before discussing the query capabilities.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap