RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


XLinq Part 1: Classes and Methods : Page 3

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.

Querying XML
XLinq provides a number of methods for querying the XElement and XDocument objects. These methods are defined in the abstract XContainer class. The key methods are shown in Table 1.

Table 1: Query Methods: The table shows common methods for querying the content of XElement and XDocument objects.
Method Description
Ancestors Returns the ancestor XML elements in the form of an IEnumerable list.
DescendantNodes Returns the descendant nodes of the XContainer.
Descendants Returns the descendant XML elements in the form of an IEnumerable list.
Element Returns the child element with the specified name.
Elements Returns the child elements of the current node. You can also supply the name of the XML element to return the child nodes that match the supplied name.
Nodes Returns the contents of the current node.

In addition to the above methods, the XContainer also exposes properties such as FirstNode, NextNode, and LastNode that allow you to get references to specific nodes contained in the current node. For example, here's how you could get a reference to the <book> element using the Element method.

   XElement bookStoreXml =  XElement.Load 
   XElement bookXml =  
Once you have a reference to the <book> element, you can easily reference any of its child nodes, including the <chapter> elements using the Elements method.

   string output =  "";
   foreach (XElement element in 
     output += element;
The preceding code loops through the collection of <chapter> elements and adds them to a string variable named output. Finally, the last line displays the contents of the string variable in a MessageBox as shown in Figure 3.

Figure 3. Looping Through Elements: Here's the output of the code that loops through the <book> element's children, selecting the <chapter> elements.
In addition to retrieving elements, you can also leverage the XElement class' Attribute and Attributes methods to retrieve attributes. These methods work similarly to the XContainer's Element and Elements methods.

Updating XML
Updating an XML element is simple and straightforward. All you need to do is navigate to the XElement whose contents you want to replace, and then invoke the ReplaceContent() method, passing in the new value for that particular element. For example, if you wanted to change the content of the chapter element, you could do the following:

    ("Modified abstract&#133;.");
You can also update an XML subtree using ReplaceContent(). For example, to update the contents of the <book> element, you could do the following:

   XElement bookStoreXml =  XElement.Load
   new XElement("chapter", 
    "Modified Abstract...",
      new XAttribute("num", "6"),
      new XAttribute("name", "Header")
   new XElement("chapter", 
    "Modified Abstract...",
   new XAttribute("num", "7"),
   new XAttribute("name", "Footer")   
The preceding code updates the entire XML subtree with new contents using the ReplaceContent() method. A similar method called SetElement() lets you update elements that contain simple content.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date