no

How to read and write an object in a textfile in C#

Long time ago I've a requirement to read sms messages from device to be written in a text file. Before I learned to serialize an object,...

Long time ago I've a requirement to read sms messages from device to be written in a text file. Before I learned to serialize an object, I was saving it in a file comma-delimited. That approach is working but sometimes it's a pain to keep track of what argument is in index x.

 So here's how I've written an object in c# to a text file. Some parts of the code are copied somewhere on the internet.

Normally we write a list of object in a text file so I've created a model class, another class that contains a list of the model class and the serializer.

1.) The model class (take note of the Serializable annotation and ISerializable interface).
[Serializable()]
    public class SmsMessageModel : ISerializable
    {
        public int Index;
        public string Recipient;
        public string Sender;
        public string Sent;
        public string Text;     

        public SmsMessageModel()
        {
        }

        public SmsMessageModel(SerializationInfo info, StreamingContext ctxt)
        {
            Index = (int)info.GetValue("Index", typeof(int));
            Recipient = (string)info.GetValue("Recipient", typeof(string));
            Sender = (string)info.GetValue("Sender", typeof(string));
            Sent = (string)info.GetValue("Sent", typeof(string));
            Text = (string)info.GetValue("Text", typeof(string));          
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Index", Index);
            info.AddValue("Recipient", Recipient);
            info.AddValue("Sender", Sender);
            info.AddValue("Sent", Sent);
            info.AddValue("Text", Text);
        }
    }
2.) The class that contains a list of the model class
[Serializable()]
    public class SmsMessageModelList : ISerializable
    {
        private List _messages;

        public List SmsMessageModels
        {
            get { return _messages; }
            set { _messages = value; }
        }

        public SmsMessageModelList()
        {
        }

        public SmsMessageModelList(SerializationInfo info, StreamingContext ctxt)
        {
            _messages = (List)info.GetValue("SmsMessageModels", typeof(List));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("SmsMessageModels", _messages);
        }
    }
3.) The serializer
public class SmsMessageSerializer
    {
        public void SerializeObject(string filename, SmsMessageModelList objectToSerialize)
        {
            Stream stream = File.Open(filename, FileMode.Create);
            var bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, objectToSerialize);
            stream.Close();
        }

        public SmsMessageModelList DeSerializeObject(string filename)
        {
            Stream stream = File.Open(filename, FileMode.Open);
            var bFormatter = new BinaryFormatter();
            var objectToSerialize = (SmsMessageModelList)bFormatter.Deserialize(stream);
            stream.Close();
            return objectToSerialize;
        }
    }
And how we implement:
var serializer = new SmsMessageSerializer();
serializer.SerializeObject(smsFile, simMessages);

var simMessages = new SmsMessageModelList();
var serializer = new SmsMessageSerializer();
simMessages.SmsMessageModels = serializer.DeSerializeObject(smsFile).SmsMessageModels;

Related

.net 7024693988535959255

Post a Comment

item