Modifying Data in an XML Document
Finally, as you would hope, LINQ to XML provides numerous ways to insert, delete, copy, and
update XML content. Adding new XElements to an existing XElement (or XDocument) is no harder than
calling the Add() method, which adds the data to the end of the element/document. As an alterna-
tive, you can call AddFirst() to add the item to the top of the element/document or AddAfterThis()/
AddBeforeThis() to insert data at a specific location.
Updating or deleting content is also very straightforward. After constructing a LINQ query
statement to identify the item (or items) you wish to tinker with, simply call ReplaceContent() (for
updating) or Remove()/RemoveContent() (for deletion of data). By way of a simple example, consider
the following code, which adds a set of new elements to the incoming XElement parameter:
static void AddNewElements(XElement doc)
{
Do'stlaringiz bilan baham: |