I need to convert the data of all the rows in XML format and then at last save it in database. But, before saving it, I again need to convert it to string format.
//These two namespaces are required to create XML Document and String Writer.
using System.Xml;
using System.IO;
protected string CreateXMLFromGrid()
{
// Created new XmlDocument to store Xml.
XmlDocument xmlDoc = new XmlDocument();
//Created the root element and added it to xmlDoc.
XmlElement root = xmlDoc.CreateElement("root");
xmlDoc.AppendChild(root);
foreach (GridViewRow Row in grdTopUpRequest.Rows)
{
CheckBox chkTopUpRequest = (CheckBox)Row.FindControl("chkRequest");
if (null != chkTopUpRequest && chkTopUpRequest.Checked)
{
XmlElement rec = xmlDoc.CreateElement("rec");
root.AppendChild(rec);
XmlElement topupid = xmlDoc.CreateElement("topupid");
topupid.InnerText = grdTopUpRequest.DataKeys[Row.RowIndex].Value.ToString();
rec.AppendChild(topupid);
XmlElement amount = xmlDoc.CreateElement("amount");
TextBox txtAmount = (TextBox)Row.FindControl("txtAmount");
amount.InnerText = txtAmount.Text.Trim();
rec.AppendChild(amount);
XmlElement remarks = xmlDoc.CreateElement("remarks");
TextBox txtRemarks = (TextBox)Row.FindControl("txtRemarks");
amount.InnerText = txtRemarks.Text.Trim();
rec.AppendChild(remarks);
}
// Creating an Object of StringWriter and assigining it to XmlTextWriter, whixh will
// copy the xml from xmlDoc to StringWriter.
StringWriter strWriter = new StringWriter();
XmlTextWriter xmlTxtWriter = new XmlTextWriter(strWriter);
xmlDoc.WriteTo(xmlTxtWriter);
return strWriter.ToString();
}
}
No comments:
Post a Comment