Aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!File.Exists(Server.MapPath("Studentdetail.xml")))
{
writefile();
}
else
{
readfile();
}
}
public void writefile()
{
string path = Server.MapPath("Studentdetail.xml");
XmlTextWriter ob = new XmlTextWriter(path, null);
ob.WriteStartDocument();
ob.WriteStartElement("Dataset");
ob.WriteStartElement("Items");
ob.WriteElementString("ID", txtid.Text);
ob.WriteElementString("UserName", txtuname.Text);
ob.WriteElementString("Password", txtpwd.Text);
ob.WriteEndElement();
ob.WriteEndElement();
ob.WriteEndDocument();
fill();
}
public void fill()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Studentdetail.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
public void readfile()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("Studentdetail.xml"));
XmlElement Item = xdoc.CreateElement("Items");
XmlElement i = xdoc.CreateElement("i");
XmlElement id = xdoc.CreateElement("ID");
XmlText xid = xdoc.CreateTextNode(txtid.Text);
XmlElement uname = xdoc.CreateElement("UserName");
XmlText xuname = xdoc.CreateTextNode(txtuname.Text);
XmlElement pwd = xdoc.CreateElement("Password");
XmlText xpwd = xdoc.CreateTextNode(txtpwd.Text);
id.AppendChild(xid);
uname.AppendChild(xuname);
pwd.AppendChild(xpwd);
Item.AppendChild(id);
Item.AppendChild(uname);
Item.AppendChild(pwd);
xdoc.DocumentElement.AppendChild(Item);
xdoc.Save(Server.MapPath("Studentdetail.xml"));
fill();
}
}
Studentdetail.xml
<?xml version="1.0" encoding="utf-8"?>
<Dataset>
<Items>
<i>
<ID>asdf</ID>
<UserName>asdf</UserName>
<Password>df</Password>
</i>
</Items>
<Items>
<i>
<ID>3</ID>
<UserName>123</UserName>
<Password>123</Password>
</i>
</Items>
<Items>
<ID>1</ID>
<UserName>2</UserName>
<Password>3</Password>
</Items>
<Items>
<ID>hitesh</ID>
<UserName>p</UserName>
<Password>p</Password>
</Items>
<Items>
<ID>1</ID>
<UserName>hitesh</UserName>
<Password>pass</Password>
</Items>
<Items>
</Items>
<Items>
</Items>
</Dataset>
Create XML Documents with Asp.net Website.
ReplyDeleteFor Learning Latest Technologies like XML with ASP.NET @ Vadodara.
Visit : www.vataliyatuitionclasses.com