How to create element using XmlDocument in asp.net

XmlDocument CreateElement()- How to create element using XmlDocument in asp.net
XmlDocumentCreateElement.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 xmlPath = Request.PhysicalApplicationPath + @"App_Data\BookList2.xml";
        if (System.IO.File.Exists(xmlPath))
        {
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "Xml Document Already Exists!";
        }
        else
        {
            XmlDocument xmlDoc = new XmlDocument();

            XmlNode declarationNode = xmlDoc.CreateXmlDeclaration("1.0", "", "");
            xmlDoc.AppendChild(declarationNode);

            XmlNode booksNode = xmlDoc.CreateElement("books");
            xmlDoc.AppendChild(booksNode);

            XmlNode bookNode = xmlDoc.CreateElement("book");

            XmlNode bookNameNode = xmlDoc.CreateElement("nameofbook");
            bookNameNode.InnerText = "Building Dashboards for Windows SharePoint Services 3.0 using SharePoint Designer 2007";
            bookNode.AppendChild(bookNameNode);

            XmlNode bookAuthorNode = xmlDoc.CreateElement("bookauthor");
            bookAuthorNode.InnerText = "Raymond Mitchell";
            bookNode.AppendChild(bookAuthorNode);

            booksNode.AppendChild(bookNode);
            xmlDoc.Save(xmlPath);
            Label1.Text = "Xml Document Created Successfully!";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>XmlDocument CreateElement()- How to create element using XmlDocument in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkSeaGreen; font-style:italic;">XmlDocument Example: Using CreateElement() Method</h2>
        <hr width="675" align="left" color="DarkSeaGreen" />
        <br /><br />
        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Bold="true"
            Text=""
            ForeColor="OrangeRed"
            Font-Names="Comic Sans MS"
            Font-Size="X-Large"
            >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Create XmlDocument With Element Using XmlDocument"
            Height="42"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>








Related asp.net example