serialization - I have a Single File And need to serialize multiple objects randomly. How can I in c#? -
I have a file and whenever any new object is created then several objects of the same class should be serialized. I can not store them in arrays because I need to sort the things that the object has to be created. Please help me
Is the serialization mechanism you are using? There may be a problem due to the XmlSerializer root node and things like namespace announcements may be, which is a bit harder to get shots - plus it is not very good in partial decalialization. BinaryFormatter is very brittle - I do not recommend it in most cases.
may be an option; This is a binary serial (using Google's "Protocol Buffers" format - efficient, portable, and version-tolerant). You can do more than one object in the stream with Serializer.SerializeWithLengthPrefix . To deselect the same items, Serializer. DIREARIALIZ ITEMS one of the deserialized items IEnumerable & lt; T & gt; - or you can easily make tryDeserializeWithLengthPrefix public (this is currently private, but the source is available.)
Just after creating it, write each object to file - worked.
If you want an example, please say - although unit tests give an overview.
This will basically be something like (untested):
using (stream s = File.Create (path)) {Serializer.SerializeWithLengthPrefix (S, Command1, prefix style, base 128, 0); ... your code etc. Serializer.SerializeWithLengthPrefix (s, commandN, PrefixStyle.Base128, 0); } ... use (stream s = File.OpenRead (path)) {foreach (command command serializer.DeserializeItems & lt; command & gt; (s, PrefixStyle.Base128, 0)) with command Something}}
Comments
Post a Comment