<%@ Page Language="C#" %>
<%@ Import NameSpace="System.Xml" %>
<script runat="server" >
void btnDiscount_Click(Object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc = (XmlDocument)bookSource.GetXmlDocument();
double discountPercent = Convert.ToInt32(txtDiscountPercent.Text);
string path = "Data/genre/book";
XmlNodeList nodeList = doc.SelectNodes(path);
for(int i=0; i< nodeList.Count ; i++)
{
XmlNode node = nodeList[i];
double price = Convert.ToDouble(node.Attributes["Price"].Value);
double discount = price * (discountPercent/100);
XmlAttribute discountAttribute = doc.CreateAttribute("Discount");
discountAttribute.Value = discount.ToString();
node.Attributes.Append(discountAttribute);
}
bookSource.Save();
bookRepeater.DataBind();
}
</script>
<html>
<head>
<title>Updating Data through XmlDataSource Control</title>
</head>
<body>
<form id="Form1" runat="server" >
<asp:XmlDataSource runat="server" ID="bookSource" XPath="Data/genre/book"
DataFile="~/Data.xml" EnableViewState="True"/>
<asp:Repeater runat="server" ID="bookRepeater" DataSourceID="bookSource" >
<ItemTemplate >
<h2><%# XPath ("@Title") %> </h2>
<b>ISBN:</b><%# XPath ("@ISBN") %> <%# XPath ("author/last-name/text()") %>
<b>Price:</b><%# XPath ("@Price") %>
<b>Discount:</b><%# XPath ("@Discount") %>
</ItemTemplate>
</asp:Repeater>
<p>
Enter the discount percentage:<asp:TextBox runat="server" ID="txtDiscountPercent" />
<asp:Button runat="server" ID="btnAddDiscount" onclick="btnDiscount_Click"
Text="Add Discount" /></p>
</form>
</body>
</html>
File: ~/Data.xml
<Data>
<genre name="Fiction">
<book ISBN="1" Title="title 1" Price="19.99" Discount="1.999">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
</genre>
<genre name="NonFiction">
<book ISBN="2" Title="title 2" Price="27.95" Discount="2.795">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
</genre>
</Data>
|