Delete All XML Elements and Attributes Nodes

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in XML in C#.

Question: What is deleting all XML Element/Attributes nodes?

In simple terms "It provides flexibility to delete all elements and attributes from XML and save the changes to the XML document." 

 

Step 1: Create a new "ASP.NET Web Application", as in:

 

Output1.jpg

Step 2: The complete code of Employee.xml looks like this:

 

<?xml version="1.0" encoding="utf-8"?>

<Employees>

  <Employee>

    <Id>1</Id>

    <FirstName>Vijay</FirstName>

    <LastName>Prativadi</LastName>

    <Age>26</Age>

  </Employee>

  <Employee>

    <Id>2</Id>

    <FirstName>Sandeep</FirstName>

    <LastName>Reddy</LastName>

    <Age>28</Age>

  </Employee>

</Employees>

 

Step 3: The complete code of webform1.aspx looks like this:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DeleteAllElementAttributesXMLApp._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Label ID="Label1" runat="server" Text="Delete All XML Elements/Attribute Nodes"

                            Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button1" runat="server" Text="Delete Nodes" Font-Names="Verdana"

                            Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</body>

</html>

 

Step 4: The complete code of webform1.aspx.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;

namespace DeleteAllElementAttributesXMLApp

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            XmlDocument document = new XmlDocument();

            document.Load(Server.MapPath("Employee.xml"));

            XmlNode objXNode = document.DocumentElement;

            objXNode.RemoveAll();

            document.Save(Server.MapPath("Employee.xml"));

            Label5.Text = "XML Elements/Attributes Deleted Successfully";

            Label5.ForeColor = System.Drawing.Color.Green;

        }

    }

}
 

Step 5: The output of the application looks like this:
 

Delete-All XML-Element-Attribut-s-Nodes.png
 

Step 6: The data deleted output of the application looks like this:
 

XML-Element-Attribut-s-Nodes.png
 

 Step 7: The complete code of Employee.xml looks like this:
 

<?xml version="1.0" encoding="utf-8"?>

<Employees>

</Employees>

I hope this article is useful for you.

 


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.