Deserializing Data
Số trang: 3
Loại file: pdf
Dung lượng: 13.46 KB
Lượt xem: 2
Lượt tải: 0
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 ]
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ìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Deserializing DataGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
74 trang 299 0 0
-
96 trang 293 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 265 0 0