- Applying XML Attributes
- Serializing Objects to a Stream
- Reading an XML Stream into a DataSet
- Summary
Reading an XML Stream into a DataSet
After the last statement runs in Listing 3, we have the array of customer objects serialized to a stream. We can reconstitute the array as a DataSet because ADO.NET DataSets speak XML. Listing 4 includes a basic Windows form that creates an array of customers, serializes, and binds those customers to a DataGrid.
Listing 4Serializing an Object to a DataSet and Binding to a DataGrid
1: using System; 2: using System.Drawing; 3: using System.Collections; 4: using System.ComponentModel; 5: using System.Windows.Forms; 6: using System.Data; 7: using System.Xml.Serialization; 8: using System.IO; 9: 10: namespace SerializeToDataSet 11: { 12: public class Form1 : System.Windows.Forms.Form 13: { 14: private System.Windows.Forms.Button button1; 15: private System.Windows.Forms.DataGrid dataGrid1; 16: private System.ComponentModel.Container components = null; 17: 18: public Form1() 19: { 20: InitializeComponent(); 21: } 22: 23: protected override void Dispose( bool disposing ) 24: { 25: if( disposing ) 26: { 27: if (components != null) 28: { 29: components.Dispose(); 30: } 31: } 32: base.Dispose( disposing ); 33: } 34: 35: [ Windows Form Designer generated code ] 36: 37: /// <summary> 38: /// The main entry point for the application. 39: /// </summary> 40: [STAThread] 41: static void Main() 42: { 43: Application.Run(new Form1()); 44: } 45: 46: Customer[] customers = null; 47: private void Form1_Load(object sender, System.EventArgs e) 48: { 49: customers = new Customer[3]; 50: customers[0] = new Customer("Amway", "http://www.amway.com/"); 51: customers[1] = 52: new Customer("Merrill-Lynch", "http://www.merrilllynch.com/"); 53: customers[2] = new Customer("Puerto Rico Ports Authority"); 54: 55: } 56: 57: private void button1_Click(object sender, System.EventArgs e) 58: { 59: XmlSerializer serializer = 60: new XmlSerializer(typeof(Customer[])); 61: 62: //MemoryStream stream = new MemoryStream(); 63: FileStream stream = 64: new FileStream("c:\\temp\\customers.xml", FileMode.CreateNew); 65: 66: serializer.Serialize(stream, customers); 67: stream.Position = 0; 68: DataSet dataset = new DataSet(); 69: dataset.ReadXml(stream); 70: dataGrid1.DataSource = dataset.Tables[0]; 71: } 72: } 73: }
The array of customers is defined on line 46 and initialized in the Form1_Load event handler on lines 4755. The button1_Click handler contains the serialization code and the additional four steps for serializing the array into a DataSet. First we reset the stream position to the start of the stream on line 67. A new DataSet is created on line 68, and line 69 invokes the DataSet.ReadXml(stream) method, passing the XML stream to the method. Finally, we indicate that the table created by the read in the DataSet (line 70) is the source for the DataGrid. Figure 1 shows the result.
Figure 1 The serialized version of the Customer array.
NOTE
The XmlSerializer also serializes public fields. However, by convention we seldom use public fields.
From Figure 1 we can see that the serializer used the XmlElement name values and only serialized the properties that had data. If you elected to serialize all values, you could pass the positional argument IsNullable=true to the XmlElement attribute, and then null properties would also be serialized. For example,
[XmlElement("Customer Phone", IsNullable=true)]
Applied to the Phone property, this would ensure that Phone was serialized even if it contained null data.