?
Solved

ASP.NET Form Edit XML Nodes

Posted on 2009-07-04
3
Medium Priority
?
791 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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