How to create comment in asp.net xml document

XmlDocument CreateComment() - How to create comment in asp.net xml document
XmlDocumentCreateComment.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\MacBooks.xml";
        if (System.IO.File.Exists(xmlFilePath))
        {
            Label1.Text = "MacBooks.xml already exists.";
        }
        else
        {
            XmlDocument xmlDoc = new XmlDocument();
            
            XmlDeclaration xmlDeclarationNode = xmlDoc.CreateXmlDeclaration("1.0","","");
            xmlDoc.AppendChild(xmlDeclarationNode);

            XmlNode comment = xmlDoc.CreateComment("This is an xml file for Mac books collection.");
            xmlDoc.AppendChild(comment);
            
            XmlNode booksNode = xmlDoc.CreateElement("books");
            xmlDoc.AppendChild(booksNode);
            
            XmlNode bookNode = xmlDoc.CreateElement("book");

            XmlAttribute bookidAttribute = xmlDoc.CreateAttribute("bookid");
            bookidAttribute.Value = "001";
            bookNode.Attributes.Append(bookidAttribute);
            
            XmlNode booktitleNode = xmlDoc.CreateElement("booktitle");
            booktitleNode.InnerText = "Beginning Mac OS X Tiger Dashboard Widget Development";
            bookNode.AppendChild(booktitleNode);

            XmlNode authorNode = xmlDoc.CreateElement("author");
            authorNode.InnerText = "Fred Terry";
            bookNode.AppendChild(authorNode);

            booksNode.AppendChild(bookNode);
            xmlDoc.Save(xmlFilePath);
            Label1.Text = "MacBooks.xml created successfully!";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>XmlDocument CreateComment() - How to create comment in asp.net xml document</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Magenta; font-style:italic;">XmlDocument Example: How to use CreateComment() method</h2>
        <hr width="575" align="left" color="OliveDrab" />
        <br /><br />
        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Bold="true"
            Text=""
            ForeColor="DarkSeaGreen"
            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 Comment Using XmlDocument"
            Height="40"
            Font-Bold="true"
            ForeColor="Olive"
            />
    </div>
    </form>
</body>
</html>








Related asp.net example