ASP.NET XmlDocument - How to use AppendChild() method

XmlDocument AppendChild() - How to use AppendChild() method in asp.net
XmlDocumentAppendChild.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Xml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Button1_Click(object sender, System.EventArgs  e)
    {
        string xmlFilePath = Request.PhysicalApplicationPath + @"App_Data\EmployeeList.xml";
        if (System.IO.File.Exists(xmlFilePath))
        {
            Label1.Text = "EmployeeList.xml already exists.";
        }
        else
        {
            XmlDocument xmlDoc = new XmlDocument();
            
            XmlDeclaration xmlDeclarationNode = xmlDoc.CreateXmlDeclaration("1.0","","");
            xmlDoc.AppendChild(xmlDeclarationNode);

            XmlNode employeesNode = xmlDoc.CreateElement("employees");
            xmlDoc.AppendChild(employeesNode);
            
            XmlNode employeeNode = xmlDoc.CreateElement("employee");

            XmlNode employeeNameNode = xmlDoc.CreateElement("employeename");
            employeeNameNode.InnerText = "Jenny Jones";
            employeeNode.AppendChild(employeeNameNode);

            XmlNode cityNode = xmlDoc.CreateElement("city");
            cityNode.InnerText = "Rome";
            employeeNode.AppendChild(cityNode);

            employeesNode.AppendChild(employeeNode);
            xmlDoc.Save(xmlFilePath);
            Label1.Text = "EmployeeList.xml created successfully!";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>XmlDocument AppendChild() - How to use AppendChild() method in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Magenta; font-style:italic;">XmlDocument Example: How to use AppendChild() method</h2>
        <hr width="575" align="left" color="Pink" />
        <br /><br />
        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Bold="true"
            Text=""
            ForeColor="SeaGreen"
            Font-Names="Comic Sans MS"
            Font-Size="X-Large"
            >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Create EmployeeList.xml XmlDocument Using XmlDocument"
            Height="40"
            Font-Bold="true"
            ForeColor="IndianRed"
            />
    </div>
    </form>
</body>
</html>








Related asp.net example