<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPat
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Book
{
if (!Page.IsPostBack)
{
string str = Request.QueryString["BookI
string xmlPath = MapPath("App_Data/BookStor
XmlDocument doc = new XmlDocument();
if (System.IO.File.Exists(xml
{
doc.Load(xmlPath);
XmlNode node = doc.SelectSingleNode("/boo
if (node != null)
{
txtBookID.Text = str;
txtTitle.Text = node.SelectSingleNode("tit
txtFirstName.Text = node.SelectSingleNode("aut
txtLastName.Text = node.SelectSingleNode("aut
txtPrice.Text = node.SelectSingleNode("pri
txtGenre.Text = node.Attributes["genre"].V
Editor1.Text = node.SelectSingleNode("det
}
}
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
string xmlPath = MapPath("App_Data/BookStor
XmlDocument doc = new XmlDocument();
//Check if the file already exists or not
if (System.IO.File.Exists(xml
{
doc.Load(xmlPath);
XmlNode bookNode = CreateBookNode(doc);
//Get reference to the book node and append the book node to it
XmlNode bookStoreNode = doc.SelectSingleNode("book
XmlNode checkExistingNode = bookStoreNode.SelectSingle
if (checkExistingNode != null)
{
bookStoreNode.ReplaceChild
}
else
{
bookStoreNode.AppendChild(
}
lblResult.Text = "XML Document has been successfully updated";
}
else
{
XmlNode declarationNode = doc.CreateXmlDeclaration("
doc.AppendChild(declaratio
XmlNode comment = doc.CreateComment("This file represents a fragment of a book store inventory database");
doc.AppendChild(comment);
XmlNode bookstoreNode = doc.CreateElement("booksto
//Append the bookstore node to the document
doc.AppendChild(bookstoreN
lblResult.Text = "XML Document has been successfully created";
}
doc.Save(xmlPath);
}
XmlNode CreateBookNode(XmlDocument
{
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
//Add all the children of the book node
XmlNode titleNode = doc.CreateElement("title")
titleNode.InnerText = txtTitle.Text;
bookNode.AppendChild(title
//Create the author node and its children
XmlNode authorNode = doc.CreateElement("author"
XmlNode firstNameNode = doc.CreateElement("first-n
firstNameNode.InnerText = txtFirstName.Text;
authorNode.AppendChild(fir
XmlNode lastNameNode = doc.CreateElement("last-na
lastNameNode.InnerText = txtLastName.Text;
authorNode.AppendChild(las
bookNode.AppendChild(autho
XmlNode priceNode = doc.CreateElement("price")
priceNode.InnerText = txtPrice.Text;
bookNode.AppendChild(price
XmlNode detailsNode = doc.CreateElement("details
detailsNode.InnerText = Editor1.Text;
bookNode.AppendChild(detai
XmlNode BookIDNode = doc.CreateElement("BookID"
BookIDNode.InnerText = txtBookID.Text;
bookNode.AppendChild(BookI
return bookNode;
}
</script>
<html xmlns="http://www.w3.org/1
<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>
Main Topics
Browse All Topics





by: jinalPosted on 2009-07-04 at 06:02:15ID: 24776781
To Load Node from XmlFile as per query string parameter.
ID"] != null) D"]; e.xml"); Path)) kstore/boo k[BookID=" + str.Trim() + "]"); le").Inner Text; hor/first- name").Inn erText; hor/last-n ame").Inne rText; ; ce").Inner Text; ; alue; ails").Inn erText; ;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Book
{
string str = Request.QueryString["BookI
string xmlPath = MapPath("App_Data/BookStor
XmlDocument doc = new XmlDocument();
if (System.IO.File.Exists(xml
{
doc.Load(xmlPath);
XmlNode node = doc.SelectSingleNode("/boo
if (node != null)
{
txtBookID.ReadOnly = true;
txtBookID.Text = str;
txtTitle.Text = node.SelectSingleNode("tit
txtFirstName.Text = node.SelectSingleNode("aut
txtLastName.Text = node.SelectSingleNode("aut
txtPrice.Text = node.SelectSingleNode("pri
txtGenre.Text = node.Attributes["genre"].V
Editor1.Text = node.SelectSingleNode("det
}
}
}
}
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.