Created
August 24, 2012 10:01
-
-
Save fresky/3448591 to your computer and use it in GitHub Desktop.
Serialize using BinaryFormatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example example = new Example(); | |
example.ID = 10; | |
example.NameList.Add(new NamePair(){Name = "name1", Value = "value1"}); | |
example.NameList.Add(new NamePair() { Name = "name2", Value = "value2" }); | |
Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create); | |
BinaryFormatter bformatter = new BinaryFormatter(); | |
Console.WriteLine("Writing Employee Information"); | |
bformatter.Serialize(stream, example); | |
stream.Close(); | |
example = null; | |
//Open the file written above and read values from it. | |
stream = File.Open("EmployeeInfo.osl", FileMode.Open); | |
bformatter = new BinaryFormatter(); | |
Console.WriteLine("Reading Employee Information"); | |
example = (Example)bformatter.Deserialize(stream); | |
stream.Close(); | |
Console.WriteLine("Employee Id: {0}", example.ID.ToString()); | |
Console.WriteLine("Employee Name: {0}", example.NameList.Count); | |
foreach (var s in example.NameList) | |
{ | |
Console.WriteLine(s.Name); | |
Console.WriteLine(s.Value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment