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 : Page 4

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
Deleting XML
The XElement class exposes the methods shown in Table 2 for deleting content from an XML element.

Table 2: The XElement class exposes several methods for deleting content from an XML element.
Method Description
Remove Removes this node from the XML tree.
RemoveAll Removes contents and attributes from the current element.
RemoveAnnotation Removes an annotation from the current node.
RemoveAttributes Removes all the attributes of the current node.
RemoveContent Removes all child nodes of the current node without removing attributes.

Now that you've had a brief look at the methods, here are a couple of examples.

To delete XML, navigate to the content you want to delete and call Remove(). For example, if you want to delete the <genre> element under the <bookstore> element:

bookStoreXml.Element("genre").Remove();

Remove() also accepts objects that support the IEnumerable interface, so for example, you can delete all the chapters under the <book> element using:

book.Elements("chapter").Remove();

You can remove all the content from an XElement with the RemoveContent() method.



bookStoreXml.Element("genre"). Element("book").RemoveContent();

XLinq Support in VB 9
The next version of VB (VB 9) takes XLinq support to the next level by allowing you to embed XML literals directly inside Visual Basic. Within the XML literals you can leave placeholders for attributes, attribute names, or attribute values, for element names by using (expression), or for child elements using the ASP.NET style syntax <%= expression %>, or <% statement %> for blocks. The following example shows how to accomplish this:

Dim text As String = "Abstract..." Dim chapterXml = _ <chapters> <chapter num="1" name="chapter"> <%= text %> </chapter> </chapters> 'Construct a new element based on the 'existing ones Dim bookStoreXml = _ <bookstore> <genre name="Fiction"> <book ISBN="10-861003-324" Title="A Tale of Two Cities" Price="19.99"> <%= chapterXml %> </book> </genre> </bookstore> MessageBox.Show(bookStoreXml.Xml)

Note that the <%= %> syntax lets you retrieve values from the text and chapterXml variables and place them inside the <chapters> and <bookstore> elements respectively.

The Visual Basic compiler takes XML literals and translates them into constructor calls to the underlying XLinq API. As a result, you can freely pass XML produced by Visual Basic to any other component that accepts XLinq values. Similarly, Visual Basic code can accept XLinq XML produced by external components.

This article has only scratched the surface XLinq's possibilities. Future installments of this article series will go into more detail about XLinq's capabilities, including XML transformation and XLinq's integration with DLinq for accessing data stored in relational databases.



Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap