We help IT Professionals succeed at work.

ASP.NET Form Edit XML Nodes

jay_eire
jay_eire asked
on
Medium Priority
891 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

Comment
Watch Question

Commented:
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.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
Thanks this is perfect.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.