0

I have created a serialization object (for example student) in C# and I saved it as a binary file in this way:

public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, 
bool append = false)
{
    using (Stream stream = File.Open(filePath, append ? FileMode.Append : 
    FileMode.Create))
    {
        var binaryFormatter = new 
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        binaryFormatter.Serialize(stream, objectToWrite);
    }
}

So now I want to change the name of the student and save the edited student object, but I do not want to delete the previous file and replace the new edited file instead of previous, but I just want to apply the changes in the original file.

It's like I want to change the name of a person, but not by killing him! and creating a person similar to the original person but with a new name.

MarkusEgle
  • 2,795
  • 4
  • 41
  • 61
A.Brit
  • 3
  • 4
  • 5
    You cant do that. Serialized data is only useful to recreate the object(s) the data represents. To make a change - deserialize, change, serialize again. Also, what the heck does python have to do with this??? Use only *relevant* tags – Ňɏssa Pøngjǣrdenlarp Jan 20 '18 at 21:28
  • 2
    I already told you: deserialize the data saved to new object or List, make the change(s) and serialize it again. When you researched the BinaryFormatter, did you not give any thought to how you would read it back or make these changes? – Ňɏssa Pøngjǣrdenlarp Jan 20 '18 at 21:52
  • The code posted here already saves your object. If you make changes, then it sounds like you have a collection of these things. Rather than saving one by one, store them in a `List` and you can save/serialize all of them all at once to the same file. Naturally when you Deserialize, it will create a new List with those same objects in it. – Ňɏssa Pøngjǣrdenlarp Jan 20 '18 at 22:02
  • i learned how to read it back . but i did not found something about make changes – A.Brit Jan 20 '18 at 22:03
  • The comment you deleted said you wanted to *make a new one just like the saved one but with a different name*. Making a new one means making a new one. If you change the name of the saved one, you wont have 2. You likely need a *collection* of them. IF you truly just want to make changes, you just change the properties on `objectToWrite` – Ňɏssa Pøngjǣrdenlarp Jan 20 '18 at 22:11

1 Answers1

0

You cannot edit a binary serialized object. You have to deserialize it in order to edit anything.

Let me suggest an alternative approach. If you're only after saving the student data in a file so you could load and use it later, you could change the serialization method from binary to XML (Serialize an object to XML) The XML format would allow you to find and edit a specific field in the file as it would be saved in a plain "as-is" form. Then you could edit it without the need for loading everything into memory and and eventually overwriting the original file (Replace part of large XML file) This blog post has a concrete example: https://blogs.msdn.microsoft.com/mfussell/2005/02/12/combining-the-xmlreader-and-xmlwriter-classes-for-simple-streaming-transformations/

Petras Purlys
  • 1,093
  • 6
  • 18
  • I saw the code on the link you introduced(), but there was no "path" to store xml file in those codes . in binaryformatter i gave a path to system to save the binary file .but in Xml Serialization i did not see any path for storing files. i want to store my xml file in @"d:\Students" (for example) . – A.Brit Jan 27 '18 at 09:04