Question Read the contents of '.SER' files in a folder and save it in xml

chetan_rajakumar

New member
Joined
Oct 28, 2014
Messages
1
Programming Experience
3-5
Hi,

I am having a requirement which states to Read the contents of '.ser' files present in a folder and save each filenames and file contents of files in that folder to an xml file.
I tried with the below code but getting Runtime.Serialization Exception --> {"The input stream is not a valid binary format. The starting contents (in bytes) are: AC-ED-00-05-73-72-00-10-6A-61-76-61-2E-75-74-69-6C ..."}. Please someone help me out to complete the stated requirement.

Getting exception @ this line string content = (string)bf.Deserialize(fileStream);


XmlWriterxmlWriter = XmlWriter.Create("C:\\TestFile.ser");
// Opens the document
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(
"SerializedFiles");
foreach (string file in Directory.EnumerateFiles(serializedFilesFolder, "*.SER"))
{
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
string content = (string)bf.Deserialize(fileStream);
//BinaryFormatter bf = new BinaryFormatter();
//ArrayList a = (ArrayList)bf.Deserialize(fileStream);
fileStream.Close();
xmlWriter.WriteStartElement(
"SerializedFile");
xmlWriter.WriteElementString(
"FileName", Path.GetFileName(file));
xmlWriter.WriteElementString(
"Content", content);
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();


I did some search for "Reading the '.ser' file in C#", but i found only the java related examples. Please let me know the correct way or some sample code to achieve the requirement.
Thanks in advance.

Regards,
Chetan.
 
You can't just deserialise an arbitrary file to an arbitrary type. The fiel would have to have been created by deserialising an object of that type in the first place, or at least carefully constructed to be in the same format as though it was.

What is a SER file? If you need to read one then presumably you know what it contains. If it's just text then you can call File.ReadAllText. If it's structured text then you can use a StreamReader to read it piece by piece. If it's not text then you can't read it as text. You need to understand what you're reading to know how to read it.
 
Back
Top Bottom