Thursday, September 27, 2007

Xml File Creation in Asp.Net


This blog is for creating xml file and insert / update values from asp.net page.

clsXMLActions.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections;
///
/// Summary description for XML_Actions
///

public class clsXMLActions
{
XmlDocument xmlDoc = new XmlDocument();
string Path = HttpContext.Current.Server.MapPath("Emp.xml");
public clsXMLActions()
{
}
private void OpenXmlFile()
{
if (System.IO.File.Exists(Path))
{
xmlDoc.Load(Path);
}
else
{
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xmlDeclaration);
XmlElement root = xmlDoc.CreateElement("Emp_Details");
xmlDoc.AppendChild(root);
}
}
public void InsertValues(Hashtable ht)
{
OpenXmlFile();
XmlNodeList Nodelst = xmlDoc.GetElementsByTagName("Emp_Details");
XmlNode Node;
bool Edited = false;
if (ht.Count > 0)
{
for (int Cnt = 0; Cnt < node =" Nodelst.Item(0).ChildNodes[Cnt];" value =" ht[" value =" ht[" edited =" true;" edited ="="" root =" xmlDoc.DocumentElement;" child1 =" xmlDoc.CreateElement(">Page1.aspx
on Button Click


clsXMLActions obj = new clsXMLActions();
Hashtable ht = new Hashtable();
ht.Add("Id", TextBox1.Text);
ht.Add("Name", TextBox2.Text);
ht.Add("Mobile", TextBox3.Text);
obj.InsertValues(ht);
Page.RegisterStartupScript("s", "<script>alert('Updated Successfully...'); </script>");


No comments: