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 appending XML data with the append method?
In simple terms "It provides flexibility to append XML data and save the changes to the XML document.".
Step 1: Create a new "ASP.NET Web Application", as in:
 
![Output1.jpg]()
 
Step 2: Add a new XML file item to the project and name it Employee.xml.
 
Step 3: The complete code of webform1.aspx looks like this:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AppendXMLDataApp._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="Append XML Data with Append Method" Font-Bold="true"
                            Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label6" runat="server" Text="Please Enter Id" Font-Size="Large" Font-Names="Verdana"
                            Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName" Font-Size="Large"
                            Font-Names="Verdana" Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Please Enter LastName" Font-Size="Large"
                            Font-Names="Verdana" Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Text="Please Enter Age" Font-Size="Large" Font-Names="Verdana"
                            Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="Button1" runat="server" Text="Append Data" 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 AppendXMLDataApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.Text)) 
            {
                Label5.Text = "Please Enter Some Values";
                Label5.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                XmlNode rootNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(rootNode);
                XmlNode employeesNode = doc.CreateElement("Employees");
                doc.AppendChild(employeesNode);
                XmlNode employeeNode = doc.CreateElement("Employee"); 
                employeesNode.AppendChild(employeeNode);
                XmlNode idNode = doc.CreateElement("Id");
                idNode.InnerText = TextBox4.Text;
                employeeNode.AppendChild(idNode);
                XmlNode firstNameNode = doc.CreateElement("FirstName");
                firstNameNode.InnerText = TextBox1.Text;
                employeeNode.AppendChild(firstNameNode);
                XmlNode lastNameNode = doc.CreateElement("LastName");
                lastNameNode.InnerText = TextBox2.Text; 
                employeeNode.AppendChild(lastNameNode);
                XmlNode ageNode = doc.CreateElement("Age");
                ageNode.InnerText = TextBox3.Text;
                employeeNode.AppendChild(ageNode);
                doc.Save(Server.MapPath("Employee.xml"));
                Label5.Text = "Data Appended Successfully";
                Label5.ForeColor = System.Drawing.Color.Green;
                TextBox4.Text = string.Empty;
                TextBox1.Text = string.Empty;
                TextBox2.Text = string.Empty; 
                TextBox3.Text = string.Empty; 
            }
        }
    }
}
 
Step 5: The output of the application looks like this:
 
![append-xml-data.png]()
 
Step 6: The data appending output of the application looks like this:
 
![data-append-xml.png]()
 
Step 7: 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>
</Employees>
I hope this article is useful for you.