Here is for the people who wanna know how to implement simple user comments form using xml.
HTML
<form id="form1" runat="server">
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="231px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="231px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Comment:
</td>
<td>
<asp:TextBox ID="TextBox3" TextMode="MultiLine" runat="server" Width="235px" Height="88px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" />
</td>
<td>
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Clear" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
Width="428px" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:BoundField DataField="comments" HeaderText="Comment" />
</Columns>
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</form>
Namespaces
using System.Xml;
using System.Data;
C#
internal int selectIndex
{
get
{
if (this.Session["Page_selectIndex"] == null)
return -1;
return Int32.Parse(this.Session["Page_selectIndex"].ToString());
}
set
{
this.Session["Page_selectIndex"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadXmlData();
}
}
private void loadXmlData()
{
DataSet myDs = new DataSet();
myDs.ReadXml(Server.MapPath(@"App_Data\info.xml"));
if (myDs.Tables.Count > 0)
{
this.GridView1.DataSource = myDs;
this.GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
selectIndex = this.GridView1.SelectedIndex;
FindXmlData(selectIndex);
}
private void FindXmlData(int selectedIndex)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath(@"App_Data\info.xml"));
XmlNodeList xmlnodelist = xmldoc.DocumentElement.ChildNodes;
XmlNode xmlnode = xmlnodelist.Item(selectedIndex);
this.TextBox1.Text = xmlnode["name"].InnerText;
this.TextBox2.Text = xmlnode["email"].InnerText;
this.TextBox3.Text = xmlnode["comments"].InnerText;
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath(@"App_Data\info.xml"));
XmlElement newelement = xmldoc.CreateElement("usercomments");
XmlElement xmlName = xmldoc.CreateElement("name");
XmlElement xmlEmail = xmldoc.CreateElement("email");
XmlElement xmlContent = xmldoc.CreateElement("comments");
xmlName.InnerText = this.TextBox1.Text.Trim();
xmlEmail.InnerText = this.TextBox2.Text.Trim();
xmlContent.InnerText = this.TextBox3.Text.Trim();
newelement.AppendChild(xmlName);
newelement.AppendChild(xmlEmail);
newelement.AppendChild(xmlContent);
xmldoc.DocumentElement.AppendChild(newelement);
xmldoc.Save(Server.MapPath(@"App_Data\info.xml"));
loadXmlData();
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
}
protected void Button4_Click(object sender, EventArgs e)
{
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
}
XML
<?xml version="1.0" encoding="utf-8"?>
<infoRoot>
<usercomments>
<name>John Hammond</name>
<email>JohnHammond@Test.com</email>
<comments>Test</comments>
</usercomments>
<usercomments>
<name>Suzanne Mathews</name>
<email>Mathews@Test.com</email>
<comments>Test</comments>
</usercomments>
</infoRoot>
Screenshot
