Friday, December 19, 2008

XML writting (ASP.Net using c#)

.





പ്രയര്‍ റിക്വസ്റ്റിനു‌ വേണ്ടി തയ്യാറാക്കുന്ന XML ഫയല്‍ ആണ് ഇത്.(prayerrequest.xml)



{?xml version="1.0" encoding="utf-8"?>
{prayerrequest>
{requesters>
{name>shibu
{request>test data
{date>2002-Jan-03
{/requesters>
{/prayerrequest>

[ { നു പകരം < ഉപയോഗിക്കുക]
ഈ XML ഫയലിലേക്ക് അടുത്ത പ്രയര്‍ റിക്വസ്റ്റ് റൈറ്റ് ചെയ്യാന്‍ പോവുകയാണ്.






.Net 2005 code :

protected void btn_submit_Click(object sender, EventArgs e)
{
try
{
if (txt_name.Text == "" txt_request.Text == "")
{
lbl_status.Text = "Please complete the form.";
}
else
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("prayerrequest.xml"));

DateTime dd;
dd = DateTime.Now.Date;
String dd2;
dd2 = dd.ToString("yyyy-MMM-dd");

XmlElement xmlrequest = xmldoc.CreateElement("requesters");
XmlElement xmlname = xmldoc.CreateElement("name");
XmlElement xmlreq = xmldoc.CreateElement("request");
XmlElement xmldate = xmldoc.CreateElement("date");

xmlname.InnerText = txt_name.Text.Trim();
xmlreq.InnerText = txt_request.Text.Trim();
xmldate.InnerText = dd2;

xmlrequest.AppendChild(xmlname);
xmlrequest.AppendChild(xmlreq);
xmlrequest.AppendChild(xmldate);

xmldoc.DocumentElement.AppendChild(xmlrequest);

xmldoc.Save(Server.MapPath("prayerrequest.xml"));

txt_name.Text = ""; txt_request.Text = "";
lbl_status.Text = "Your prayer request successfully added";
lbl_status.Text = "";
}
}
catch
{
lbl_status.Text = "Sorry, unable to process request. Please try again.";
}

}


.Net 2008 code :

protected void btn_submit_Click(object sender, EventArgs e)
{
try
{
if (txt_name.Text == "" txt_request.Text == "")
{
lbl_status.Text = "Please complete the form.";
}
else
{
XDocument xmldoc = XDocument.Load(Server.MapPath("prayerrequest.xml"));

DateTime dd;
dd = DateTime.Now.Date;
String dd2;
dd2 = dd.ToString("yyyy-MMM-dd");

xmldoc.Element("prayerrequest").Add(new XmlElement("requesters", new XElement("name", txt_name.Text), new XElement("request", txt_request.Text), new XElement("date", dd2)));
xmldoc.CreateElement("prayerrequest").AppendChild(new XmlElement("requesters", new XmlElement("name", txt_name.Text), new XmlElement("request", txt_request.Text), new XmlElement("date", dd2)));

txt_name.Text = ""; txt_request.Text = "";
lbl_status.Text = "Your prayer request successfully added";
readthexmldata();
lbl_status.Text = "";
}
}
catch
{
lbl_status.Text = "Sorry, unable to process request. Please try again.";
}

}


.

No comments: