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

XmlDocument PrependChild() - How to use PrependChild() method in asp.net
XmlDocumentPrependChild.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\ITBookList.xml";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlFilePath);

        XmlNode booksNode = xmlDoc.SelectSingleNode("books");

        XmlNode bookNode = xmlDoc.CreateElement("book");
        
        XmlAttribute categoryAttribute = xmlDoc.CreateAttribute("Category");
        categoryAttribute.Value = "Mac";
        bookNode.Attributes.Append(categoryAttribute);

        XmlNode idNode = xmlDoc.CreateElement("id");
        idNode.InnerText = "001";
        bookNode.AppendChild(idNode);

        XmlNode nameNode = xmlDoc.CreateElement("name");
        nameNode.InnerText = "Beginning Xcode";
        bookNode.AppendChild(nameNode);

        XmlNode authorNode = xmlDoc.CreateElement("author");
        authorNode.InnerText = "James Bucanek";
        bookNode.AppendChild(authorNode);

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

        booksNode.PrependChild(bookNode);
        xmlDoc.Save(xmlFilePath);
        Label1.Text = "Xml document updated successfully!";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>XmlDocument PrependChild() - How to use PrependChild() method in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Crimson; font-style:italic;">XmlDocument Example: How to use PrependChild() method</h2>
        <hr width="575" align="left" color="Pink" />
        <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="Update Xml Document Using XmlDocument"
            Height="40"
            Font-Bold="true"
            ForeColor="IndianRed"
            />
    </div>
    </form>
</body>
</html>
ITBookList.xml
ITBookList.xml source code here.

   










Related asp.net example