Save form data to XML file : Form Save « XML « ASP.Net

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » XML » Form Save 
Save form data to XML file

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string xmlPath = MapPath("Books.xml");
        XmlDocument doc = new XmlDocument();
        //Check if the file already exists or not
        if (System.IO.File.Exists(xmlPath))
        {
            doc.Load(xmlPath);
            XmlNode bookNode = CreateBookNode(doc);
            //Get reference to the book node and append the book node to it
            XmlNode bookStoreNode = doc.SelectSingleNode("bookstore");
            bookStoreNode.AppendChild(bookNode);
            lblResult.Text = "XML Document has been successfully updated";
        }
        else
        {            
            XmlNode declarationNode = doc.CreateXmlDeclaration("1.0""""");
            doc.AppendChild(declarationNode);
            XmlNode comment = doc.CreateComment("This file represents a fragment of a book store inventory database");
            doc.AppendChild(comment);            
            XmlNode bookstoreNode = doc.CreateElement("bookstore");
            XmlNode bookNode = CreateBookNode(doc);                        
            //Append the book node to the bookstore node            
            bookstoreNode.AppendChild(bookNode);
            //Append the bookstore node to the document
            doc.AppendChild(bookstoreNode);
            lblResult.Text = "XML Document has been successfully created";
        }
        doc.Save(xmlPath);
    }

    XmlNode CreateBookNode(XmlDocument doc)
    {
        XmlNode bookNode = doc.CreateElement("book");
        //Add the genre attribute to the book node
        XmlAttribute genreAttribute = doc.CreateAttribute("genre");
        genreAttribute.Value = txtGenre.Text;        
        bookNode.Attributes.Append(genreAttribute);

        //Add all the children of the book node            
        XmlNode titleNode = doc.CreateElement("title");
        titleNode.InnerText = txtTitle.Text;
        bookNode.AppendChild(titleNode);

        //Create the author node and its children
        XmlNode authorNode = doc.CreateElement("author");
        XmlNode firstNameNode = doc.CreateElement("first-name");
        firstNameNode.InnerText = txtFirstName.Text;
        authorNode.AppendChild(firstNameNode);

        XmlNode lastNameNode = doc.CreateElement("last-name");
        lastNameNode.InnerText = txtLastName.Text;
        authorNode.AppendChild(lastNameNode);

        bookNode.AppendChild(authorNode);

        XmlNode priceNode = doc.CreateElement("price");
        priceNode.InnerText = txtPrice.Text;
        bookNode.AppendChild(priceNode);

        return bookNode;
    }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Creating an XmlDocument</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>        
        <table>
            <tr>
                <td colspan="2" style="width: 174px; height: 40px">
                    <b>Book Details:</b>
                </td>                               
            </tr>
            <tr>
                <td style="width: 101px; height: 44px">
                    Genre:
                </td>
                <td style="width: 204px; height: 44px">
                    <asp:TextBox ID="txtGenre" runat="server" Width="201px"></asp:TextBox>
                </td>
            </tr>
             <tr>
                <td style="width: 101px; height: 44px">
                    Title:
                </td>
                <td style="width: 204px; height: 44px">
                    <asp:TextBox ID="txtTitle" runat="server" Width="201px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 101px; height: 41px">
                    First Name:
                </td>
                <td style="width: 204px; height: 41px">
                    <asp:TextBox ID="txtFirstName" runat="server" Width="201px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 101px; height: 41px">
                    Last Name:
                </td>
                <td style="width: 204px; height: 41px">
                    <asp:TextBox ID="txtLastName" runat="server" Width="201px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 101px; height: 41px">
                    Price:
                </td>
                <td style="width: 204px; height: 41px">
                    <asp:TextBox ID="txtPrice" runat="server" Width="201px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="width: 101px; height: 41px">
                    <asp:Button Text="Save" runat="server" ID="btnSave" Width="95px" OnClick="btnSave_Click"/>
                </td>                
            </tr>
            <tr>
                <td colspan="2" style="width: 101px; height: 41px">
                    <asp:Label Text="Save" runat="server" ID="lblResult" Width="295px"/>
                </td>                
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.