Solved

ASP.NET Form Edit XML Nodes

Posted on 2009-07-04
3
768 Views
Last Modified: 2013-11-11
Hi Guys,
I have a ASP.NET form that is adding books to my XML file, I want to change this around so that I can edit existing books already in my XML file using the form, Im guessing that if i pass the BookID to the page as in BookEdit.aspx?BookID=1 that it should return the xml nodes to the various fields to allow me to update them, how do i go about doing this? some example code would be a great help.

below is my code and XML file, im using the editor from the AJAX Toolkit .net framework 3.5

Thanks
Jay
<%@ Page Language="C#" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %>

<%@ Import Namespace="System.Xml" %>

 

<script runat="server">

    protected void btnSave_Click(object sender, EventArgs e)

    {

        string xmlPath = MapPath("App_Data/BookStore.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);

 

        XmlNode detailsNode = doc.CreateElement("details");

        detailsNode.InnerText = Editor1.Content;

        bookNode.AppendChild(detailsNode);

 

        XmlNode BookIDNode = doc.CreateElement("BookID");

        BookIDNode.InnerText = txtBookID.Text;

        bookNode.AppendChild(BookIDNode);

 

        return bookNode;

    }

    

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" 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 style="width: 101px; height: 41px">

                    BookID:

                </td>

                <td style="width: 204px; height: 41px">

                    <asp:TextBox ID="txtBookID" runat="server" Width="201px"></asp:TextBox>

                </td>

            </tr>

            

                        <tr>

                <td style="width: 101px; height: 41px">

                    Details:

                </td>

                <td style="width: 204px; height: 41px">

                

        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        

        <cc1:Editor 

            ID="Editor1" 

            Width="450px"  

            Height="200px"

            runat="server"/>

 

                    

                </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>

 

<?xml version="1.0" encoding="utf-8"?>

<bookstore>

  <book genre="Science">

    <title>Book 1</title>

    <author>

      <first-name>Fname 1</first-name>

      <last-name>Lname 1</last-name>

    </author>

    <price>25.00</price>

    <details>Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 &lt;span style="font-weight: bold; color: #ff0000;"&gt;Details 1 Details 1&lt;/span&gt; Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 Details 1 </details>

    <BookID>1</BookID>

  </book>

  <book genre="History">

    <title>Book 2</title>

    <author>

      <first-name>Fname 2</first-name>

      <last-name>Lname 2</last-name>

    </author>

    <price>55.00</price>

    <details>Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 &lt;span style="font-weight: bold; color: #ff0000;"&gt;Details 2 Details 2&lt;/span&gt; Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 Details 2 </details>

    <BookID>2</BookID>

  </book>

  <book genre="Math">

    <title>Book 3</title>

    <author>

      <first-name>Fname 3</first-name>

      <last-name>Lname 3</last-name>

    </author>

    <price>25.00</price>

    <details>Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 &lt;span style="font-weight: bold; color: #ff0000;"&gt;Details 3 Details 3&lt;/span&gt; Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 Details 3 </details>

    <BookID>3</BookID>

  </book>

  <book genre="Art">

    <title>Book 3</title>

    <author>

      <first-name>Fname 3</first-name>

      <last-name>Lname 3</last-name>

    </author>

    <price>28.00</price>

    <details>Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 &lt;span style="font-weight: bold; color: #ff0000;"&gt;Details 4 Details 4&lt;/span&gt; Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 Details 4 </details>

    <BookID>4</BookID>

  </book>

</bookstore>

Open in new window

0
Comment
Question by:jay_eire
  • 2
3 Comments
 
LVL 15

Expert Comment

by:jinal
ID: 24776781
To Load Node from XmlFile as per query string parameter.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["BookID"] != null)
        {
            string str = Request.QueryString["BookID"];
            string xmlPath = MapPath("App_Data/BookStore.xml");
            XmlDocument doc = new XmlDocument();
            if (System.IO.File.Exists(xmlPath))
            {
                doc.Load(xmlPath);
                XmlNode node = doc.SelectSingleNode("/bookstore/book[BookID=" + str.Trim() + "]");
                if (node != null)
                {
                    txtBookID.ReadOnly = true;
                    txtBookID.Text = str;
                    txtTitle.Text = node.SelectSingleNode("title").InnerText;
                    txtFirstName.Text = node.SelectSingleNode("author/first-name").InnerText;
                    txtLastName.Text = node.SelectSingleNode("author/last-name").InnerText; ;
                    txtPrice.Text = node.SelectSingleNode("price").InnerText; ;
                    txtGenre.Text = node.Attributes["genre"].Value;
                    Editor1.Text = node.SelectSingleNode("details").InnerText; ;
                }
            }
        }
    }

Pass  EditForm.aspx?BookID=1 . It will Load node with Book ID 1.

Please take note that i removed Editor Control . So just replace with that otherwise it works fine.
0
 
LVL 15

Accepted Solution

by:
jinal earned 500 total points
ID: 24776804
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["BookID"] != null)
        {
            if (!Page.IsPostBack)
            {
                string str = Request.QueryString["BookID"];
                string xmlPath = MapPath("App_Data/BookStore.xml");
                XmlDocument doc = new XmlDocument();
                if (System.IO.File.Exists(xmlPath))
                {
                    doc.Load(xmlPath);
                    XmlNode node = doc.SelectSingleNode("/bookstore/book[BookID=" + str.Trim() + "]");
                    if (node != null)
                    {

                        txtBookID.Text = str;
                        txtTitle.Text = node.SelectSingleNode("title").InnerText;
                        txtFirstName.Text = node.SelectSingleNode("author/first-name").InnerText;
                        txtLastName.Text = node.SelectSingleNode("author/last-name").InnerText; ;
                        txtPrice.Text = node.SelectSingleNode("price").InnerText; ;
                        txtGenre.Text = node.Attributes["genre"].Value;
                        Editor1.Text = node.SelectSingleNode("details").InnerText; ;
                    }
                }
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string xmlPath = MapPath("App_Data/BookStore.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");
            XmlNode checkExistingNode = bookStoreNode.SelectSingleNode("book[BookID=" + txtBookID.Text.Trim() + "]");            
            if (checkExistingNode != null)
            {
                bookStoreNode.ReplaceChild(bookNode, checkExistingNode);
            }
            else
            {
                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");
           
            //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);
 
        XmlNode detailsNode = doc.CreateElement("details");
        detailsNode.InnerText = Editor1.Text;
        bookNode.AppendChild(detailsNode);
 
        XmlNode BookIDNode = doc.CreateElement("BookID");
        BookIDNode.InnerText = txtBookID.Text;
        bookNode.AppendChild(BookIDNode);
 
        return bookNode;
    }
   
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" 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 style="width: 101px; height: 41px">
                    BookID:
                </td>
                <td style="width: 204px; height: 41px">
                    <asp:TextBox ID="txtBookID" runat="server" Width="201px"></asp:TextBox>
                </td>
            </tr>
           
                        <tr>
                <td style="width: 101px; height: 41px">
                    Details:
                </td>
                <td style="width: 204px; height: 41px">
                    <asp:TextBox ID="Editor1" runat="server" TextMode="MultiLine" Rows="5"></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>


0
 
LVL 7

Author Closing Comment

by:jay_eire
ID: 31599750
Thanks this is perfect.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now