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.
||Removes this node from the XML tree.
||Removes contents and attributes from the current element.
||Removes an annotation from the current node.
||Removes all the attributes of the current node.
||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>
also accepts objects that support the IEnumerable interface, so for example, you can delete all the chapters under the <book>
You can remove all the content from an XElement with the 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 = _
<chapter num="1" name="chapter">
<%= text %>
'Construct a new element based on the 'existing ones
Dim bookStoreXml = _
Title="A Tale of Two Cities"
<%= chapterXml %>
Note that the <%= %>
syntax lets you retrieve values from the text
variables and place them inside the <chapters>
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.