Reputation: 3252
I have my List<Employee>
object, I want to store this object to file stream. (Which would be not easily readable by human)
Also, I want to retrieve List<Employee>
from stream to display list of employee on UI (I'm using .NET Windows Forms Application)
I also want to update specific record in file steam.
Could anybody please let me know that I would I do this?
If serialization is the only way to do this, please let me know how do I use it with regards to updating specific record information.
Thanks in advance.
Upvotes: 2
Views: 6600
Reputation: 12381
With serialization you'll have to deserialize the entire list and then serialize it back to perform an update operation.
If you don't want the output to be human-readable, BinaryFormatter
is a way to go. However, if you really want to make it unreadable for random people, consider applying some encoding algorithm to your serialized data like RSA.
Example of using BinaryFormatter
for serializing:
using(FileStream fs = File.Open(FileName, FileMode.Create)) {
try
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, list);
}
catch (SerializationException)
{
// error handling
}
}
And deserializing:
using(FileStream fs = File.OpenRead(FileName)) {
try
{
BinaryFormatter bf = new BinaryFormatter();
var list = (List<int>)bf.Deserialize(fs);
}
catch (SerializationException)
{
// Error handling
}
}
EDIT: changed the code examples a little
Upvotes: 4
Reputation: 40863
Instead of going with Serialization, another option would be to go with a NoSql solution such as db4o or any of the ones listed in this question.
Here is a blog post that talks about how to use db4o. (based on mvc but could be used in a winform application as well.
This would allow you to leverage the power of linq to query your 5000 records easily and update the object without much trouble.
Upvotes: 3