ASP.NET XPathNavigator - How to update xml document

XPathNavigator - How to update xml document in asp.net
UpdateXMLDocument.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>

<!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\ITBookList.xml";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);
        XPathNavigator xPathNavigator = xmlDoc.CreateNavigator();
        int bookCount = xPathNavigator.Select("//book").Count;
        xPathNavigator.MoveToChild("books", "");
        xPathNavigator.MoveToChild("book", "");
        for (int i = 0; i < bookCount; i++)
        {
            xPathNavigator.MoveToChild("price","");
            double bookDiscount = xPathNavigator.ValueAsDouble * (.25);
            xPathNavigator.CreateAttribute("", "discountamount","", bookDiscount.ToString());
            xPathNavigator.MoveToParent();
            xPathNavigator.MoveToNext();
        }
        xPathNavigator.MoveToRoot();
        Literal1.Text = xPathNavigator.OuterXml;
        Label1.Text = "XmlDocument updated successfully!";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>XPathNavigator - How to update xml document in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">XPathNavigator Example: How To Update XML Document</h2>
        <hr width="575" align="left" color="DarkBlue" />
        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Bold="true"
            ForeColor="Crimson"
            Font-Names="Comic Sans MS"
            >
        </asp:Label>
        <br /><br />
        <asp:Literal 
            ID="Literal1"
            runat="server"
            Mode="Encode"
            >
        </asp:Literal>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Update XMLDocument"
            Height="40"
            Font-Bold="true"
            ForeColor="DarkBlue"
            />
    </div>
    </form>
</body>
</html>
ITBookList.xml
ITBookList.xml source code here.

   






Related asp.net example