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

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, 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;




- Hibernate Search Faceting
With the document available from Hibernate Search (https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting), we should be able to implement a faceting example that returns the faceting field and its count. In the example...

- How To Upload A File In Rest Api
This is how I define the method: @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) ActionStatus uploadFile(@MultipartForm FileUploadForm form); And then the implementation: public ActionStatus uploadFile(FileUploadForm form) { File file...

- Create A Primary Key Column With Sequence Generated Value In Hibernate
For example you have a class User: package org.kalidad.seamexercises.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.SequenceGenerator; ...

- Sending And Receiving Xml Through Http Post
Usually, it's easier done on dotnet webservice but there are times when your client wants to do this using Http Post. So I've created 2 test pages, one that will request and with for a response while the other will accept the request and return...

- Download Image Given A Url In C# Using The System.net.httpwebrequest Class
Saves image url object into the local machine /// /// Saves the list of url object into the output directory /// /// list of url path /// root directory that will be used for saving internal static void PhotoDownloader(ArrayList aList, string outputDir)...



Tech-Today








.