ASP.NET - How to create xml document programmatically

XmlDocument- How to create xml document programmatically using XmlDocument
CreateXmlDocument.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\BookList.xml";
        if (System.IO.File.Exists(xmlPath))
        {
            Label1.Text = "Xml File 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 nameNode = xmlDoc.CreateElement("name");
            nameNode.InnerText = "Integrating Crystal Reports into an ASP.NET Application";
            bookNode.AppendChild(nameNode);

            XmlNode authorNode = xmlDoc.CreateElement("author");
            authorNode.InnerText = "Vincent Varallo";
            bookNode.AppendChild(authorNode);

            XmlNode priceNode = xmlDoc.CreateElement("price");
            priceNode.InnerText = "6.99";
            bookNode.AppendChild(priceNode);

            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- How to create xml document programmatically using XmlDocument</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Crimson; font-style:italic;">XmlDocument Example: Create XML Document Programmatically</h2>
        <hr width="650" 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 Using XmlDocument"
            Height="42"
            Font-Bold="true"
            ForeColor="DeepSkyBlue"
            />
    </div>
    </form>
</body>
</html>








Related asp.net example