Working with XML Records and DataGrid with C# and ASP.NET
In this article you will learn how to Read/Write the XML, Add XML Node / Record, Delete XML Node / Record and Display XML Data in DataGrid .
1.Create a xml-file and copy the following data.<?xml version="1.0" encoding="utf-8"?>
<Movies>
<Movie>
<ID>1</ID>
<Title>The Avengers</Title>
<Director>Joss Whedon</Director>
<Year>2012</Year>
<Rating>8.7</Rating>
</Movie>
<Movie>
<ID>2</ID>
<Title>Dark Shadows</Title>
<Director>Tim Burton</Director>
<Year>2012</Year>
<Rating>6.6</Rating>
</Movie>
<Movie>
<ID>3</ID>
<Title>Men in Black III</Title>
<Director>Barry Sonnenfeld</Director>
<Year>2012</Year>
<Rating>NA</Rating>
</Movie>
<Movie>
<ID>4</ID>
<Title>Looser</Title>
<Director>Lennes</Director>
<Year>2014</Year>
<Rating>7</Rating>
</Movie>
</Movies>
2.Html Body Tag---including datalist code
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Movie ID:
</td>
<td>
<asp:TextBox ID="txtMovieID" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Movie Title:
</td>
<td>
<asp:TextBox ID="txtMovieTitle" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Movie Director:
</td>
<td>
<asp:TextBox ID="txtMovieDirector" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Movie Year:
</td>
<td>
<asp:TextBox ID="txtMovieYear" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Movie Rating:
</td>
<td>
<asp:TextBox ID="txtMovieRating" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
<br />
<asp:Button ID="btnAddMovie" runat="server" Text="Add Movie" OnClick="btnAddMovie_Click" />
<asp:Button ID="btnRemoveMovie" runat="server" Text="Remove Movie" OnClick="btnRemoveMovie_Click" />
<br />
<br />
<br />
<asp:DataGrid ID="dgData" runat="server" AutoGenerateColumns="true">
<HeaderStyle Font-Bold="true" />
</asp:DataGrid>
<br />
</div>
</form>
</body>
3.Source code :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DisplayMovies();
}
}
protected void btnAddMovie_Click(object sender, EventArgs e)
{
XmlDocument objXMLDoc = ReadXML();
XmlElement objElement = objXMLDoc.CreateElement("Movie");
XmlElement ID = objXMLDoc.CreateElement("ID");
ID.InnerText = txtMovieID.Text;
XmlElement Title = objXMLDoc.CreateElement("Title");
Title.InnerText = txtMovieTitle.Text;
XmlElement Director = objXMLDoc.CreateElement("Director");
Director.InnerText = txtMovieDirector.Text;
XmlElement Year = objXMLDoc.CreateElement("Year");
Year.InnerText = txtMovieYear.Text;
XmlElement Rating = objXMLDoc.CreateElement("Rating");
Rating.InnerText = txtMovieRating.Text;
objElement.AppendChild(ID);
objElement.AppendChild(Title);
objElement.AppendChild(Director);
objElement.AppendChild(Year);
objElement.AppendChild(Rating);
objXMLDoc.DocumentElement.AppendChild(objElement);
//Save XML
WriteXML(objXMLDoc);
//Display Movie List again
DisplayMovies();
}
private void DisplayMovies()
{
DataSet oDs = GetXMLDataSet();
if (oDs != null && oDs.Tables.Count > 0 && oDs.Tables[0].Rows.Count > 0)
{
dgData.DataSource = oDs;
dgData.DataBind();
}
}
protected void btnRemoveMovie_Click(object sender, EventArgs e)
{
bool blnFlag = false;
XmlDocument objXMLDoc = ReadXML();
XmlNodeList lstMovies = objXMLDoc.GetElementsByTagName("Movie");
foreach (XmlNode objNode in lstMovies)
{
XmlNodeList objNodeList = objNode.ChildNodes;
foreach (XmlNode objInnerNode in objNodeList)
{
if (objInnerNode.InnerText == txtMovieTitle.Text.ToString())
{
objXMLDoc.DocumentElement.RemoveChild(objNode);
blnFlag = true;
break;
}
}
if (blnFlag)
break;
}
//Save XML
WriteXML(objXMLDoc);
//Display Movie List again
DisplayMovies();
}
private DataSet GetXMLDataSet()
{
DataSet objDS = new DataSet();
objDS.ReadXml(Server.MapPath("~/XMLFile.xml"));
return objDS;
}
private XmlDocument ReadXML()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/XMLFile.xml"));
return xmlDoc;
}
private void WriteXML(XmlDocument objDoc)
{
objDoc.Save(Server.MapPath("~/XMLFile.xml"));
}
Source:http://mrbool.com/working-with-xml-records-and-datagrid-with-c-and-asp-net/24610
No comments:
Post a Comment