Just Focus on Techonology

Lead My Life

将数据存入XML文件后帮入控件(Saving Data into Xml Files and Retriving into Winform controls)

Something maybe useful here.Let's go.

A DataSet represents storage of data for efficient manipulation of data for an application. A dataset can contain multiple tables of results. The tables in a dataset can represent data from many different sources (for example, from databases, from XML files, spreadsheets, and so on). Once the data is in a dataset, it can be related in a homogeneous format as if it had come from a single source. A dataset affords greater mobility of data between the application and data tier of your applications. A dataset provides a powerful way to exchange data with other components of an application. Datasets include extensive support for features such as serializing data as XML and reading and writing XML Schemas.A dataset enables usage of data periodically and frequently without having to seek the database object again and again. This reusability feature allows for greater data interaction in an application.

The following program has a form that acts as a front-end and has four controls in it - a combo box, a checked list box, a listview and a listbox. The button is simply there to provide an event for the procedure to be executed. This is just a sample program and the casual usage of string constants and naming conventions for the objects may please be ignored because the real import is in the method - ReadWriteXmlDocument(), which is called from the OpenDialog event.

The Namespace, Main and the Class modules have been left out from the following program as it only adds up to additional space. The ReadWriteXmlDocument() method creates a DataSet object with one DataTable called "myTable" in it. The next step is to create two columns for the table as you would for any database and set their datatypes and sizes. After creating the columns,the DataColumn objects have to be physically added to the DataTable with the .Add method which takes the names of the columns to be added, as its parameters. Now that the table is created, add a DataRow object to it that will hold the values. The adding of data is done through a for loop and to physically add the data into the table, the AcceptChanges method of the DataSet object is invoked.

Now that the DataSet is created, the next step is to use it in an Xml format for which we create two filestream objects - fOutxml and fInXml. The data in our dataset is used to write into the Xml format with - firstDataSet.WriteXml(xmlWriter);

The above command writes the data into the file encapsulated into the xmlWriter object. To read from the Xml file, the ReadXml method is used. The DisplayData(...) method is simply used to display the data into a text box to evaluate that data has been read properly from the Xml file. Since there are three entities in a DataSet - DataTable, DataRow and DataColumn,there are three for each iterations in the DisplayData() method to browse through all the records in the dataset.

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7
  8  private System.Windows.Forms.ListBox listBox1;
  9  private System.Windows.Forms.CheckedListBox checkedListBox1;
 10  private System.Windows.Forms.ComboBox comboBox1;
 11  private System.Windows.Forms.OpenFileDialog openFileDialog1;
 12  private System.Windows.Forms.Button button1;
 13  private System.Windows.Forms.ListView listView1;
 14  private System.Windows.Forms.ImageList imageList1;
 15  private System.ComponentModel.IContainer components;
 16  private System.Windows.Forms.TextBox textBox1;
 17  private string nameofFile="";
 18
 19 private void button1_Click(object sender, System.EventArgs e)
 20  {
 21   comboBox1.Items.Add("Jv");
 22   listBox1.Items.Add("Ravichandran");
 23   checkedListBox1.Items.Add("Top Guru");   
 24   int totalLstItems=listBox1.Items.Count;
 25   int loopCtr=0;
 26   while (loopCtr<totalLstItems)
 27   {
 28    listBox1.SelectedIndex=loopCtr;
 29
 30    listView1.Items.Add(listBox1.Text.ToString());
 31    loopCtr++;
 32   }

 33
 34  }

 35  private void ReadWriteXMLDocument()
 36  {
 37   // Creating a DataSet with one table.
 38   DataSet firstDataSet = new DataSet("myDataSet");
 39   firstDataSet.Namespace= "myXmlDataSet";
 40   DataTable myTable = new DataTable("myTable");
 41
 42   // Creating two columns in the table.
 43   DataColumn column1 = new DataColumn("fName", Type.GetType("System.String"));
 44
 45   DataColumn column2 = new DataColumn("lName",Type.GetType("System.String"));
 46
 47   // Adding the columns to the table.
 48   myTable.Columns.Add(column1);
 49   myTable.Columns.Add(column2);
 50
 51   // Add the table to the dataset object.
 52   firstDataSet.Tables.Add(myTable);
 53
 54   // Add rows according to number of items in ListBox.
 55   DataRow newRow;
 56   int loopCtr=0;
 57   for(loopCtr = 0; loopCtr < listBox1.Items.Count; loopCtr++)
 58   {
 59    newRow = myTable.NewRow();
 60    newRow["fName"]= "First name - " + comboBox1.Text.ToString();
 61    newRow["lName"]= "Last name - " + listBox1.Text.ToString();
 62    myTable.Rows.Add(newRow);
 63   }
   
 64   // Commit new changes into the dataset.
 65   firstDataSet.AcceptChanges();
 66
 67   string xmlFilename = nameofFile;
 68   // A new FileStream object.
 69   System.IO.FileStream fOutXml = new System.IO.FileStream
 70    (xmlFilename, System.IO.FileMode.Create);
 71   // New XmlTextWriter to write the file.
 72   System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
 73    (fOutXml, System.Text.Encoding.Unicode);
 74   // Use WriteXml to write the document.
 75   firstDataSet.WriteXml(xmlWriter);
 76   // Close the Out FileStream object.
 77   fOutXml.Close();
 78     
 79   // Dispose this DataSet.
 80   firstDataSet.Dispose();
 81
 82   // Create a new DataSet.
 83   DataSet secondDataSet = new DataSet("Second DataSet");
 84     
 85   // New FileStream object to read XML schema.
 86   System.IO.FileStream fInXml = new System.IO.FileStream
 87    (xmlFilename, System.IO.FileMode.Open);
 88   // XmlTextReader object 'myXmlReader' to read the file.
 89   System.Xml.XmlTextReader myXmlReader =
 90    new System.Xml.XmlTextReader(fInXml);
 91   // Encapsulate the XML document into the DataSet.
 92   secondDataSet.ReadXml(myXmlReader);
 93   // Close the XmlTextReader object.
 94   myXmlReader.Close();
 95
 96   // Print out values of each table in the DataSet using the
 97   // function defined below.
 98   DisplayData(secondDataSet,"New DataSet");
 99  }

100
101  private void DisplayData(DataSet ds, string dsName)
102  {
103   int i=0;
104   foreach(DataTable t in ds.Tables)
105   {
106     listBox1.Items.Add(t.TableName.ToString());
107    foreach(DataRow r in t.Rows)
108    {
109     foreach(DataColumn c in t.Columns)
110     {
111      textBox1.Text+=r[c].ToString();
112      i++;
113     }
     
114    }
   
115   }

116  }

117
118  private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
119  {
120   openFileDialog1.ShowDialog();
121   nameofFile=openFileDialog1.FileName;
122   ReadWriteXMLDocument();
123  }

posted on 2006-06-26 20:27  ColinYang  阅读(406)  评论(0编辑  收藏  举报

导航