Create DocumentFragment : Xml Node « XML « ASP.Net

ASP.Net
1. ADO.net Database
2. Asp Control
3. Collections
4. Components
5. Data Binding
6. Development
7. HTML Control
8. Mobile Control
9. Page
10. Request
11. Response
12. Server
13. Session Cookie
14. User Control and Master Page
15. Validation by Control
16. Validation by Function
17. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » XML » Xml Node 
Create DocumentFragment

<%--
Code Revised from
       
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback)
by Thiru Thangarathinam 

# Publisher: Wrox (January 182006)
# Language: English
# ISBN: 0764596772
--%>


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

<script runat="server">   
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string xmlPath = MapPath("Books2.xml");
        XmlDocument doc = new XmlDocument();
        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);
        }
        else
        {            
            XmlNode declarationNode = doc.CreateXmlDeclaration("1.0""""");
            doc.AppendChild(declarationNode);
            XmlNode comment = doc.CreateComment("Comments Here");
            doc.AppendChild(comment);            
            XmlNode bookstoreNode = doc.CreateElement("bookstore");
            XmlNode bookNode = CreateBookNode(doc);                        
            bookstoreNode.AppendChild(bookNode);
            doc.AppendChild(bookstoreNode);
        }
        lblResult.Text = "XML Document has been successfully created";
        doc.Save(xmlPath);
    }

    XmlNode CreateBookNode(XmlDocument doc)
    {
        XmlDocumentFragment docFragment = doc.CreateDocumentFragment();
        docFragment.InnerXml = "<book genre='" + txtGenre.Text + "'>" 
                             "<title>" + txtTitle.Text +" </title>" +
                               "<author><first-name>" + txtFirstName.Text + "</first-name>" +
                               "<last-name>" + txtLastName.Text + "</last-name></author>" +
                               "<price>" + txtPrice.Text + "</price></book>";
        return docFragment;
    }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Creating an XmlDocumentFragment</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 runat="server" ID="lblResult" Width="295px"/>
                </td>                
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

           
       
Related examples in the same category
w__ww.__j__av_a2___s__.c___om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.