Updating Data through XmlDataSource Control : XmlDataSource « XML « ASP.Net






Updating Data through XmlDataSource Control

<%@ 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>

 








Related examples in the same category

1.asp:Xml datasource
2.Caching XML Data in an XmlDataSource Control
3.Create asp XmlDataSource
4.Binding XML Data from an XmlDataSource Control
5.Using Inline XML Data in an XmlDataSource Control
6.Binding XML Data from Other Sources
7.Programmatically Creating an XmlDataSource Control
8.Handling XmlDataSource Events
9.Fill XmlDataSource with your code
10.XmlDataSource and XML document
11.Use ItemTemplate and XPath to display data from XmlDataSource
12.Using the XmlDataSource control to consume an RSS feed
13.Enter an XML filename or raw XML starting with