Jesper Tverskov, July 14, 2008

Remove XML with W3C DOM 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 remove elements and attributes in W3C DOM. 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 renaming 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 remove elements and attributes.

1. Remove element

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

<?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 remove an element we must first create an XmlDocument object and load the XML document into it, then create an XmlNode object and navigate to the parent element of the element we want to remove, then create another XmlNode object and navigate to the element we want to remove.

We then remove the second XmlNode object from the first XmlNode object and save the XmlDocument object.

2. W3C DOM

2.1 remove-xml-w3cdom.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="remove-xml-w3cdom.aspx.cs" Inherits="remove_xml_w3cdom" %>

2.2 remove-xml-w3cdom.aspx.cs

using System;
using System.Xml;

public partial class remove_xml_w3cdom : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(Server.MapPath("products.xml"));

    XmlNode xmlnode = xmldoc.SelectSingleNode("products");
    XmlNode xmlnode2 = xmldoc.SelectSingleNode("products/product[@id = 'p3']");
    
    xmlnode.RemoveChild(xmlnode2);
    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. Remove attribute

Let us say that we only want to remove the "id" attribute with the "p3" value. Removing attributes are known to cause problems for many users. Attributes are not children of their parent element (!): we must choose properties and methods carefully. See: Accessing Attributes in the DOM, at MSDN.

The easiest way is similar to removing elements but we must create an XmlElement object for the parent of the attribute instead of just an XmlNode object, and an XmlAttribute object for the attribute. Note that when we create the XmlElement object using "xmldoc.SelectSingleNode", we must do it explicitly adding "(XmlElement)" in front of xmldoc.

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("products.xml"));

XmlElement xmlelement = (XmlElement)xmldoc.SelectSingleNode("products/product[@id = 'p3']");
XmlAttribute xmlattribute = xmlelement.GetAttributeNode("id");

xmlelement.RemoveAttributeNode(xmlattribute);
xmldoc.Save(Server.MapPath("products.xml"));

4. Remove XML in a namespace

See: Read XML with W3C DOM for how to use XmlNameSpaceManager.

Updated 2008-08-08