Jesper Tverskov, July 27, 2008

Rename 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 rename 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 rename elements and attributes.

1. Rename element

I use the following XML document, products.xml, in many of my XML tutorials. How could we rename the product element having an attribute named "id" with a "p3" value? We want to rename the product element "test".

<?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>

To rename an element we first create an XDocument object and load the XML document, then we create an XElement for the old element. The "oldXElement.Name" can then be renamed directly.

To make it easy to find nodes in the XML document, I prefer to use XPath, "System.Xml.XPath", also when using LINQ to XML.

2. LINQ to XML

2.1 rename-xml-linq.aspx

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

2.2 rename-xml-linq.aspx.cs

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

public partial class rename_xml_linq : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
    {
      XDocument xmldoc = XDocument.Load(Server.MapPath("products-2.xml"));
      XElement oldXElement = xmldoc.XPathSelectElement("products/product[@id = 'p3']");
      oldXElement.Name = XName.Get("test", oldXElement.Name.NamespaceName);

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

      Response.Redirect("products-2.xml"); // load file in browser
    }
}

LINQ to XML takes less code than W3C DOM 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. Rename attribute

Let us say that we only want to rename the "id" attribute with the "p3" value. We want to rename it "test" keeping the original value. It is not possible to rename an attribute as elegant as renaming an element. Start creating an XElement object for the parent of the attribute, create a string to keep the value of the attribute, remove the attribute and create a new attribute with the stored value.

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

public partial class rename_xml_linq_a : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    XDocument xmldoc = XDocument.Load(Server.MapPath("products.xml"));
    XElement parentElement = xmldoc.XPathSelectElement("products/product[@id = 'p3']");
    //value of attribute we want to rename
    string value = parentElement.Attribute("id").Value;

    //remove attribute we want to rename
    parentElement.Attribute("id").Remove();

    //add attribute to make it look like renaming
    parentElement.SetAttributeValue("test", value);

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

4. Rename XML in a namespace

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

Updated 2008-11-14