using System.Xml;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// Open the XML doc
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(Server.MapPath("books.xml"));
System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;
// Create new XML element and populate its attributes
System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("entry");
XmlElement name = myXmlDocument.CreateElement("Name");
name.InnerText = Server.HtmlEncode(TextBoxName.Text).ToString();
myXmlElement.AppendChild(name);
XmlElement location=myXmlDocument.CreateElement("Location");
location.InnerText=Server.HtmlEncode(TextBoxLocation.Text).ToString();
myXmlElement.AppendChild(location);
XmlElement email=myXmlDocument.CreateElement("Email");
email.InnerText=Server.HtmlEncode(TextBoxEmail.Text).ToString();
myXmlElement.AppendChild(email);
XmlElement gender = myXmlDocument.CreateElement("Gender");
gender.InnerText=Server.HtmlEncode(DropDownListGender.SelectedItem.Text).ToString();
myXmlElement.AppendChild(gender);
// Insert data into the XML doc and save
myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
myXmlDocument.Save(Server.MapPath("books.xml"));
// Re-bind data since the doc has been added to
BindData();
LabelMessage.Text = "Record inserted Successfully Inside the XML File...";
TextBoxName.Text = "";
TextBoxLocation.Text = "";
TextBoxEmail.Text = "";
}
void BindData()
{
XmlTextReader myXmlReader = new XmlTextReader(Server.MapPath("books.xml"));
myXmlReader.Close();
}
}
No comments:
Post a Comment