Methods For Transforming Data To A XML File

Here are the different methods to transform a data file to a XML Document,
 
Method 1
 
The first idea of converting a piece of data which are in a text files to a Xml Document is by using a DataSet. By using a DataSet, we can bind that data to the DataGrid and do the updation, deletion and addition operations. Even though the final output is the same Xml file, we have the Dataset in between to manipulate the data.
 
Example 
  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Xml;  
  12. using System.IO;  
  13. namespace XMLWork1 {  
  14.     /// <summary>  
  15.     /// Summary description for WebForm1.  
  16.     /// </summary>  
  17.     public class WebForm1: System.Web.UI.Page {  
  18.         protected System.Web.UI.WebControls.Label Label1;  
  19.         protected System.Web.UI.WebControls.Label Label2;  
  20.         protected System.Web.UI.WebControls.TextBox txtName;  
  21.         protected System.Web.UI.WebControls.TextBox txtPlace;  
  22.         protected System.Web.UI.WebControls.Button Fill;  
  23.         DataSet dataSet = new DataSet("dataSet");  
  24.         DataTable dt = new DataTable();  
  25.         protected System.Web.UI.WebControls.DataGrid DataGrid1;  
  26.         DataRow dr;  
  27.         private void Page_Load(object sender, System.EventArgs e) {  
  28.             if (!IsPostBack) {  
  29.                 try {  
  30.                     dataSet.ReadXml(Server.MapPath("sample.xml"));  
  31.                 } catch (Exception ex) {  
  32.                     if (dataSet.Tables.Count == 0) {  
  33.                         dataSet.WriteXml(Server.MapPath("sample.xml"));  
  34.                     }  
  35.                     Response.Write(ex.Message);  
  36.                 }  
  37.             }  
  38.         }  
  39.         #region Web Form Designer generated code  
  40.         override protected void OnInit(EventArgs e) {  
  41.             //  
  42.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.  
  43.             //  
  44.             InitializeComponent();  
  45.             base.OnInit(e);  
  46.         }  
  47.         /// <summary>  
  48.         /// Required method for Designer support - do not modify  
  49.         /// the contents of this method with the code editor.  
  50.         /// </summary>  
  51.         private void InitializeComponent() {  
  52.             this.Fill.Click += new System.EventHandler(this.Fill_Click);  
  53.             this.Load += new System.EventHandler(this.Page_Load);  
  54.         }  
  55.         #endregion  
  56.         private void Fill_Click(object sender, System.EventArgs e) {  
  57.             dataSet.ReadXml(Server.MapPath("sample.xml"));  
  58.             if (dataSet.Tables.Count == 0) {  
  59.                 DataColumn dc1 = new DataColumn("Name");  
  60.                 DataColumn dc2 = new DataColumn("Place");  
  61.                 dataSet.Tables.Add(dt);  
  62.                 dataSet.Tables[0].Columns.Add(dc1);  
  63.                 dataSet.Tables[0].Columns.Add(dc2);  
  64.             }  
  65.             dr = dataSet.Tables[0].NewRow();  
  66.             dr["Name"] = txtName.Text;  
  67.             dr["Place"] = txtPlace.Text;  
  68.             dataSet.Tables[0].Rows.Add(dr);  
  69.             dataSet.AcceptChanges();  
  70.             dataSet.WriteXml(Server.MapPath("sample.xml"));  
  71.             BindData();  
  72.         }  
  73.         private void BindData() {  
  74.             DataGrid1.DataSource = dataSet;  
  75.             DataGrid1.DataBind();  
  76.         }  
  77.     }  
  78. }  
Here in this, on the load method, we are reading the xml file which is created using a DataSet. If it is not getting created it is been catched and it is created. When the Fill Button in the form is clicked after entering the values - Name and Place, a new row is been created and a xml file is generated using the DataSet.WriteXml method. The DataTable, DataColumn, DataRow are used for this purposes.
 
Then for binding the DataGrid, we use the DataGird.DataSource =the dataset.
 
Method 2
 
The second method is to use the XmlDocument. This class uses the xmlTextWriter and using that the xmlfile is obtained.
 
Example 
  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Xml;  
  12. namespace XMLData {  
  13.     /// <summary>  
  14.     /// Summary description for WebForm1.  
  15.     /// </summary>  
  16.     public class WebForm1: System.Web.UI.Page {  
  17.         protected System.Web.UI.WebControls.DataGrid DataGrid1;  
  18.         protected System.Web.UI.WebControls.Button Fill;  
  19.         protected System.Web.UI.WebControls.TextBox txtPlace;  
  20.         protected System.Web.UI.WebControls.TextBox txtName;  
  21.         protected System.Web.UI.WebControls.Label Label2;  
  22.         protected System.Web.UI.WebControls.Label Label1;  
  23.         private void Page_Load(object sender, System.EventArgs e) {  
  24.             if (!IsPostBack) {  
  25.                 // Put user code to initialize the page here  
  26.             }  
  27.         }  
  28.         #region Web Form Designer generated code  
  29.         override protected void OnInit(EventArgs e) {  
  30.             //  
  31.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.  
  32.             //  
  33.             InitializeComponent();  
  34.             base.OnInit(e);  
  35.         }  
  36.         /// <summary>  
  37.         /// Required method for Designer support - do not modify  
  38.         /// the contents of this method with the code editor.  
  39.         /// </summary>  
  40.         private void InitializeComponent() {  
  41.             this.Fill.Click += new System.EventHandler(this.Fill_Click);  
  42.             this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);  
  43.             this.Load += new System.EventHandler(this.Page_Load);  
  44.         }  
  45.         #endregion  
  46.         private void Fill_Click(object sender, System.EventArgs e) {  
  47.             try {  
  48.                 string filename = Server.MapPath("sample2.xml");  
  49.                 XmlDocument xmlDoc = new XmlDocument();  
  50.                 try {  
  51.                     xmlDoc.Load(filename);  
  52.                 } catch (System.IO.FileNotFoundException) {  
  53.                     //if file is not found, create a new xml file  
  54.                     XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);  
  55.                     xmlWriter.Formatting = Formatting.Indented;  
  56.                     xmlWriter.WriteProcessingInstruction("xml""version='1.0' encoding='UTF-8'");  
  57.                     xmlWriter.WriteStartElement("Root");  
  58.                     //If WriteProcessingInstruction is used as above,  
  59.                     //Do not use WriteEndElement() here  
  60.                     //xmlWriter.WriteEndElement();  
  61.                     //it will cause the <Root></Root> to be <Root />  
  62.                     xmlWriter.Close();  
  63.                     xmlDoc.Load(filename);  
  64.                 }  
  65.                 XmlNode root = xmlDoc.DocumentElement;  
  66.                 XmlElement childNode = xmlDoc.CreateElement("Employee");;  
  67.                 root.AppendChild(childNode);  
  68.                 XmlElement childNode2 = xmlDoc.CreateElement("Name");  
  69.                 XmlText textNode1 = xmlDoc.CreateTextNode(txtName.Text);  
  70.                 childNode2.AppendChild(textNode1);  
  71.                 childNode.AppendChild(childNode2);  
  72.                 XmlElement childNode3 = xmlDoc.CreateElement("Place");  
  73.                 XmlText textNode2 = xmlDoc.CreateTextNode(txtPlace.Text);  
  74.                 childNode3.AppendChild(textNode2);  
  75.                 childNode.AppendChild(childNode3);  
  76.                 xmlDoc.Save(filename);  
  77.             } catch (Exception ex) {  
  78.                 Response.Write(ex.ToString());  
  79.             }  
  80.         }  
  81.     }  
  82. }  
The main point to be noted here is that we are not using any dataset here. Both the two methods use and produce the same xml file as output. But the difference is that, we cannot bind it to a DataGrid as we are not using any DataSets to be indulged in this. There are many other methods also ie., by using XMLDataDocument also.


Similar Articles