Jesper Tverskov, August 7, 2008

Update XML with LINQ to XML in ASP.NET

In ASP.NET we have two main APIs for editing XML: W3C DOM and LINQ to XML. In this tutorial we look at how to update elements and attributes in LINQ to XML. There are many more properties and methods and ways of achieving the same result than shown below. You must look up the proper documentation for all the details.

Editing XML basically consists of the following: adding, deleting or replacing elements and attributes, and updating content of elements and values of attributes. An API for editing also has properties and methods for comments, processing instructions, etc, but in this tutorial we only look at how to update elements and attributes.

1. Update element

I use the following XML document, products.xml, in many of my XML tutorials. How could we update the "country" element of the "p3" product to become "China" or "<p>China</p>"?

<?xml version="1.0" encoding="utf-8"?>
<products>
  <product id="p1">
    <name>Delta</name>
    <price>800</price>
    <stock>4</stock>
    <country>Denmark</country>
  </product>
  ...
  <product id="p3">
    <name>Alfa</name>
    <price>1200</price>
    <stock>19</stock>
    <country>Germany</country>
  </product>
  ...
</products>

In LINQ to XML we can simply use the Value property both for "text" and for "markup + text". Or we can use the methods SetValue() for "text" or SetElementValue() for "markup + text". I find the use of the word "Value" confusing for elemets because the standard for XML (Recommendation) tells us that an element has content (not a value) but an attribute is a name-value pair.

The following code shows four ways of updating an element. Three of the four must be deleted or out-commented if you want to use the code for real.

2. LINQ to XML

2.1 update-xml-linq.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="update-xml-linq.aspx.cs" Inherits="update_xml_linq" %>

2.2 update-xml-linq.aspx.cs

using System;
using System.Xml.Linq;
using System.Xml.XPath;

public partial class update_xml_linq : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    XDocument xmldoc = XDocument.Load(Server.MapPath("products.xml"));
    XElement xElement = xmldoc.XPathSelectElement("products/product[@id = 'p3']/country");

    // NOTE: 3 of 4 solutions must be deleted or out-commented

    // a) Value = text
    xElement.Value = "China";

    // b) Value = markup + text
    xElement.Value = "<p>China</p>";

    // c) SetValue = text
    xElement.SetValue("China");

    // d) SetElementValue = markup + text
    xElement.Nodes().Remove();
    xElement.SetElementValue("p", "China");

    xmldoc.Save(Server.MapPath("products.xml"));
    Response.Redirect("products.xml"); // load file in browser
  }
}

The W3C DOM API takes more code than LINQ to XML but the nice thing about W3C DOM is that it is a standard API implemented by most programming languages. We can even read or edit an HTML document more or less the same using the HTML version of W3C DOM.

3. Update attribute

Let us say that we want to change the "p3" value of the "id" attribute to "p10".

XDocument xmldoc = XDocument.Load(Server.MapPath("products.xml"));
XElement xElement = xmldoc.XPathSelectElement("products/product[@id = 'p3']");
xElement.Attribute("id").Value = "p10";

xmldoc.Save(Server.MapPath("products.xml"));

4. Update XML in a namespace

See: Read XML with LINQ to XML for how to use XNamespace class.

Updated 2008-08-07