|
||||||
| CSharp Tutorials Tutorials for C# |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
C# SERIALIZATION TECHNIQUES When programming with object oriented languages you may need to transfer data through a network, or between applications. The .Net environment gives you the opportunity to accomplish this task without too much hassle. Suppose you have an object and you must pass it to another application. How do you do that? You only need to serialize the object either in binary or in xml format and your object is ready to be transferred. In this tutorial we will see an example of such a serialization in a binary format. We will create an object in a custom serializable class and then we will serialize it in a binary file. We then deserialize the file and extract its parameters so as to verify that they have been transferred correctly. 2. GRAPHICAL USER INTERFACE Create a new visual C# project named Serialization. Create a form as the one shown in picture 1. Notice that the textboxes on the right side are disabled. ![]() The user will first enter the details in the corresponding textboxes on the left. Then through the serialization technique the textboxes information will be passed to the textboxes on the right. 3. CODE DEVELOPMENT We first start with adding the necessary “Using..” statements in our project to include the appropriate namespaces: Code:
using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; Code:
[Serializable]
private class Person
{
public string Name, Surname, Title, FathName;
}
Code:
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Person Bob = new Person();
Bob.Name = textBox1.Text;
Bob.Surname = textBox2.Text;
Bob.Title = textBox3.Text;
Bob.FathName = textBox4.Text;
FileStream filestream = new
FileStream(@"C:\output11.txt", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(filestream, Bob);
filestream.Close();
button1.Enabled = true;
}
Code:
private void button2_Click(object sender, EventArgs e)
{
FileStream filestream2 = new FileStream(@"C:\output11.txt", FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
Person JohnDoe = new Person();
JohnDoe = (Person)bf2.Deserialize(filestream2);
textBox5.Text = JohnDoe.Name;
textBox6.Text = JohnDoe.Surname;
textBox7.Text = JohnDoe.Title;
textBox8.Text = JohnDoe.FathName;
filestream2.Close();
}
![]() Go to C:\Output11.txt and open the file. You will not see a normal notepad file beacause the data is in binary and not text format. Instead, you should see something like the data in Picture3. ![]() Press the deserialize button to open the binary file,deserialize it and read its contents to the righ side textboxes. The output of he second object is the same as the input ( first object) as it can be seen from picture4. ![]() Binary formatted objects can only be read by .Net Framework-based applications. You can also serialize with an XML based formatter, the so-called SoapFormatter. For a transmission across a network the objects created with this formatter are less likely to be rejected from a firewall. Finally, .Net gives you the opportunity to serialize directly to XML format but it is beyond the scope of this tutorial. Questions/Comments? Post your questions or comments here. You can also create a new thread in the appropriate forum with your question.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
![]() |
| Tags |
| c# serialization, c# tutorials |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Visual Studio 2008 C# Compressing | Jordan | CSharp Tutorials | 0 | 05-13-2008 04:52 PM |
| Visual Studio 2008: C# Hello World Tutorial | Jordan | CSharp Tutorials | 1 | 02-27-2008 07:48 AM |
| Tutorial: Visual Studio 2008 Obfuscating with Dotfuscator | Jordan | Tutorials, Classes and Code | 0 | 02-08-2008 02:01 PM |