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

XmlDocument CreateXmlDeclaration()- How to use CreateXmlDeclaration() method
CreateXmlDeclaration.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\BookList1.xml";
        if (System.IO.File.Exists(xmlPath))
        {
            Label1.Text = "Xml Document Already Exists";
        }
        else
        {
            XmlDocument xmlDoc = new XmlDocument();

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

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

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

            XmlNode bookNameNode = xmlDoc.CreateElement("bookname");
            bookNameNode.InnerText = ".NET Domain-Driven Design with C#: Problem - Design - Solution";
            bookNode.AppendChild(bookNameNode);

            XmlNode priceNode = xmlDoc.CreateElement("price");
            priceNode.InnerText = "39.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 CreateXmlDeclaration()- How to use CreateXmlDeclaration() method</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:HotPink; font-style:italic;">XmlDocument Example: Using CreateXmlDeclaration() Method</h2>
        <hr width="675" 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 XmlDocument Using XmlDocument"
            Height="42"
            Font-Bold="true"
            ForeColor="SkyBlue"
            />
    </div>
    </form>
</body>
</html>








Related asp.net example