Click here to Skip to main content
15,916,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

I have an application that contains xml file.I am converted this xml file into dataset.After that i want to insert,update,delete the xml values in the dataset.How can I do that one.Below I has send the code.Plz clear my doubt.

///authors.xml///
<authors_table>
  <authors>
    <au_id>172-32-1176</au_id>
    <au_lname>White</au_lname>
    <au_fname>Johnson</au_fname>
    <phone>408 496-7223</phone>
    <address>10932 Bigge Rd.</address>
    <city>Menlo Park</city>
    <state>CA</state>
    <zip>94025</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>213-46-8915</au_id>
    <au_lname>Green</au_lname>
    <au_fname>Margie</au_fname>
    <phone>415 986-7020</phone>
    <address>309 63rd St. #411</address>
    <city>Oakland</city>
    <state>CA</state>
    <zip>94618</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>238-95-7766</au_id>
    <au_lname>Carson</au_lname>
    <au_fname>Cheryl</au_fname>
    <phone>415 548-7723</phone>
    <address>589 Darwin Ln.</address>
    <city>Berkeley</city>
    <state>CA</state>
    <zip>94705</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>267-41-2394</au_id>
    <au_lname>Hunter</au_lname>
    <au_fname>Anne</au_fname>
    <phone>408 286-2428</phone>
    <address>22 Cleveland Av. #14</address>
    <city>San Jose</city>
    <state>CA</state>
    <zip>95128</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>274-80-9391</au_id>
    <au_lname>Straight</au_lname>
    <au_fname>Dean</au_fname>
    <phone>415 834-2919</phone>
    <address>5420 College Av.</address>
    <city>Oakland</city>
    <state>CA</state>
    <zip>94609</zip>
    <contract>true</contract>
  </authors>
</authors_table>


///xmltodatasetpageedit.aspx///

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xmltodatasetpageedit.aspx.cs" Inherits="WebApplication4.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
          >
            <columns>
                <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
                <asp:TemplateField HeaderText="au_id">
                    <edititemtemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("au_id") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button1" runat="server" Text='<%# Bind("au_id") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="au_lname">
                    <edititemtemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("au_lname") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button2" runat="server" Text='<%# Bind("au_lname") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="au_fname">
                    <edititemtemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("au_fname") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button3" runat="server" Text='<%# Bind("au_fname") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="phone">
                    <edititemtemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("phone") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button4" runat="server" Text='<%# Bind("phone") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="address">
                    <edititemtemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("address") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button5" runat="server" Text='<%# Bind("address") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="city">
                    <edititemtemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("city") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button6" runat="server" Text='<%# Bind("city") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="state">
                    <edititemtemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("state") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button7" runat="server" Text='<%# Bind("state") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="zip">
                    <edititemtemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("zip") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button8" runat="server" Text='<%# Bind("zip") %>' />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="contract">
                    <edititemtemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("contract") %>'>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Button ID="Button9" runat="server" Text='<%# Bind("contract") %>' />
                    </itemtemplate>
                
                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
            </columns>
        
    </div>
    <br />
    <br />
    <asp:Button ID="Button10" runat="server" onclick="Button10_Click" 
        style="height: 26px" Text="Button" />
    </form>
</body>
</html>
Posted
Updated 14-Mar-11 18:32pm
v3
Comments
Albin Abel 15-Mar-11 0:38am    
Additional notes to my answer: as you are using template fields, you need to handle RowEditing and RowUpdating events to update the dataset. Analyse the dataset how your data populate into datatables

Bind the dataset to a bindable control, most probably datagridview?. This bound controls update the data in the dataset on edit mode. You can have a separate user interface to enter new data to the dataset.

Basically those XML elements are arranged into tables according to the data strructure of your xml.

Now write back the xml using the dataset's WirteXml() method
 
Share this answer
 
You could start with this[^] easy sample.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900