Danh mục

Deserializing Data

Số trang: 3      Loại file: pdf      Dung lượng: 13.46 KB      Lượt xem: 2      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Team LiB ] Recipe 5.5 Deserializing Data Problem You have a DataSet that has been serialized and written to a file. You want to recreate the DataSet from this file. Solution Use the serializer objects Deserialize( ) method and cast the result as a DataSet
Nội dung trích xuất từ tài liệu:
Deserializing Data[ Team LiB ]Recipe 5.5 Deserializing DataProblemYou have a DataSet that has been serialized and written to a file. You want to recreate theDataSet from this file.SolutionUse the serializer objects Deserialize( ) method and cast the result as a DataSet.The sample code loads a file stream containing a previously serialized DataSet in aspecified format and deserializes it to recreate the original DataSet.The C# code is shown in Example 5-5.Example 5-5. File: DeserializeForm.cs// Namespaces, variables, and constantsusing System;using System.Windows.Forms;using System.IO;using System.Data;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization.Formatters.Soap;using System.Xml.Serialization;private OpenFileDialog ofd;// . . .private void goButton_Click(object sender, System.EventArgs e){ // Create and open the stream for deserializing. Stream stream = null; try { stream = File.Open(fileNameTextBox.Text, FileMode.Open, FileAccess.Read);}catch(Exception ex){ MessageBox.Show(ex.Message, Deserializing Data, MessageBoxButtons.OK, MessageBoxIcon.Error); return;}// Deserialize the DataSet from the stream.DataSet ds = null;try{ if (xmlReadRadioButton.Checked) { ds = new DataSet( ); ds.ReadXml(stream); } else if (xmlSerializerRadioButton.Checked) { XmlSerializer xs = new XmlSerializer(typeof(DataSet)); ds = (DataSet)xs.Deserialize(stream); } else if(soapRadioButton.Checked) { SoapFormatter sf = new SoapFormatter( ); ds = (DataSet)sf.Deserialize(stream); } else if(binaryRadioButton.Checked) { BinaryFormatter bf = new BinaryFormatter( ); ds = (DataSet)bf.Deserialize(stream); }}catch (System.Exception ex){ MessageBox.Show(ex.Message, Deserializing Data, MessageBoxButtons.OK, MessageBoxIcon.Error); return;}finally{ stream.Close( );} // Bind the DataSet to the grid. dataGrid.DataSource = ds.DefaultViewManager; MessageBox.Show(Deserialization complete., Deserializing Data, MessageBoxButtons.OK, MessageBoxIcon.Information);}private void fileDialogButton_Click(object sender, System.EventArgs e){ // File dialog to save file if(xmlReadRadioButton.Checked || xmlSerializerRadioButton.Checked) ofd.Filter = XML files (*.xml)|*.xml; else if(soapRadioButton.Checked) ofd.Filter = SOAP files (*.soap)|*.soap; else if(binaryRadioButton.Checked) ofd.Filter = Binary files (*.bin)|*.bin; ofd.Filter += |All files (*.*)|*.*; ofd.FilterIndex = 0; if (ofd.ShowDialog( ) == DialogResult.OK) fileNameTextBox.Text = ofd.FileName;}DiscussionThis sample deserializes any of the serialized DataSet objects from Recipe 5.4.The sample allows the user to select a file and specify a serialization type. Theappropriate serializing object is created and in the case of the XmlSerializer object, itstype is specified in the constructor. The Deserialize( ) method of the serializer object isthen used to deserialize the file stream into an object graph. This is then cast to a DataSetto complete the deserialization.See the discussion in Recipe 5.4 for more information about the serialization and theformatter classes that can serialize ADO.NET objects.[ Team LiB ]

Tài liệu được xem nhiều: