linq
  1. linq-modifying-xml-data

Modifying XML Data using LINQ to XML

LINQ to XML provides a powerful set of methods for modifying XML data. In this tutorial, you'll learn how to modify existing XML data using LINQ to XML.

Syntax

XElement.SetAttributeValue(string name, object value);
XElement.SetValue(object value);
XElement.Add(object content);
XElement.RemoveNodes();

Example

Let's assume we have an XML document as follows:

<customers>
  <customer>
    <id>101</id>
    <name>John</name>
    <address>
      <street>No. 123 Main Street</street>
      <city>Anytown</city>
    </address>
  </customer>
  <customer>
    <id>102</id>
    <name>Lisa</name>
    <address>
      <street>No. 456 Pine Street</street>
      <city>Somewhere</city>
    </address>
  </customer>
</customers>

Modifying Attribute Values

To modify attribute values, you can use the SetAttributeValue method as shown below:

XDocument xdoc = XDocument.Load("customers.xml");

var query = from c in xdoc.Descendants("customer")
            where c.Element("name").Value == "John"
            select c;

foreach (XElement customerElement in query)
{
   customerElement.SetAttributeValue("status", "preferred");
}

xdoc.Save("customers.xml");

In the above example, we are modifying the status attribute value of the customer whose name is John.

Modifying Element Values

To modify element values, you can use the SetValue method as shown below:

XDocument xdoc = XDocument.Load("customers.xml");

var query = from c in xdoc.Descendants("customer")
            where c.Element("name").Value == "Lisa"
            select c;

foreach (XElement customerElement in query)
{
    customerElement.Element("name").SetValue("New Lisa");
}

xdoc.Save("customers.xml");

In the above example, we are modifying the name element value of the customer whose name is Lisa.

Adding Elements

To add elements, you can use the Add method as shown below:

XDocument xdoc = XDocument.Load("customers.xml");

var query = from c in xdoc.Descendants("customers")
            select c;

foreach (XElement customerElement in query)
{
    customerElement.Add(new XElement("customer", 
                          new XElement("id", 103),
                          new XElement("name", "David"),
                          new XElement("address",
                              new XElement("street", "New street"),
                              new XElement("city", "New city"))
                          ));
}

xdoc.Save("customers.xml");

In the above example, we are adding a new customer element to the customers element.

Removing Elements

To remove elements, you can use the RemoveNodes method as shown below:

XDocument xdoc = XDocument.Load("customers.xml");

var query = from c in xdoc.Descendants("customer")
            where c.Element("id").Value == "102"
            select c;

foreach (XElement customerElement in query)
{
    customerElement.RemoveNodes();
}

xdoc.Save("customers.xml");

In the above example, we are removing all the child elements of the customer element whose id is 102.

Output

After the above code is executed, the updated XML will be:

<customers>
  <customer status="preferred">
    <id>101</id>
    <name>John</name>
    <address>
      <street>No. 123 Main Street</street>
      <city>Anytown</city>
    </address>
  </customer>
  <customer>
    <id>102</id>
    <name>New Lisa</name>
    <address>
      <street>No. 456 Pine Street</street>
      <city>Somewhere</city>
    </address>
  </customer>
  <customer>
    <id>103</id>
    <name>David</name>
    <address>
      <street>New street</street>
      <city>New city</city>
    </address>
  </customer>
</customers>

Explanation

In the above syntax, we use various methods provided by LINQ to XML to modify XML data. SetAttributeValue is used to modify attribute values, SetValue is used to modify element values, Add is used to add elements, and RemoveNodes is used to remove elements from the XML document.

Use

We can use LINQ to XML to modify XML data in various scenarios such as updating online customer orders, modifying product catalogs, modifying employee records, modifying customer feedback surveys, and many others.

Important Points

  • The LINQ to XML API provides a powerful set of methods to modify XML documents.
  • LINQ to XML queries can be used to retrieve specific elements and attributes based on specific conditions.
  • Modifying an XML document's elements and attributes is a straightforward process with LINQ to XML.

Summary

In this tutorial, we learned how to modify XML data using LINQ to XML. We have seen various methods provided by LINQ to XML to modify XML data. Modifying an XML document's elements and attributes using LINQ to XML is a straightforward process.

Published on: