ASP.NET Form Edit XML Nodes

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

LVL 7
jay_eireAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jinalCommented:
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
jinalCommented:
<%@ 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jay_eireAuthor Commented:
Thanks this is perfect.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.