none
Read the contents of '.SER' files in a folder and save it in xml RRS feed

  • Question

  • 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 inDirectory.EnumerateFiles(serializedFilesFolder, "*.SER"))
    {
    FileStream fileStream = newFileStream(file, FileMode.Open, FileAccess.Read);
    BinaryFormatter bf = newBinaryFormatter();
    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.

    Chetan Rajakumar

    Tuesday, October 28, 2014 9:39 AM

Answers

  • You might have to encode the content of the ser file:

    foreach (string file in Directory.EnumerateFiles("", "*.SER"))
    			{
    				byte[] content = File.ReadAllBytes( file );
    				xmlWriter.WriteStartElement("SerializedFile");
    				xmlWriter.WriteElementString("FileName", Path.GetFileName(file));
    				xmlWriter.WriteElementString("Content", System.Convert.ToBase64String( content ) );
    				xmlWriter.WriteEndElement();
    			 }

    Tuesday, October 28, 2014 11:02 AM
  • Thanks Roach, I had to encode the content in order to create the xml file.

    I had included only 2(*.ser) files out of 40+ files and the size of created xml file 700+ KB. I should compress the xml after creating it right?

    When i try to recreate the '*.ser' in local machine, i have to extract the compressed xml content and traverse the node and create the '*.ser' file. While creating '*.ser', do i need to decode the encode content so that i can get the exact '*.ser' file saved locally ?

    Regards,

    Chetan.


    Chetan Rajakumar

    Tuesday, October 28, 2014 11:36 AM

All replies

  • Hi Chetan,

    how did these *.ser files get generated? If they where generated using Java with a plattform dependent format, then you will not be able to deserialize using .Net BinaryFormatter since it will not compatible with the java serialisation format.

    Also, why deserialize into a string? Is this supposed to be the xml? If you want to create a xml representation of whats in the .ser file and within the .ser file is a binary format, then you will have to deserialize the binary format into an object tree, cast this object tree into an appropriate class and serialize this instance using for example a XmlSerializer.


    • Edited by M.Roach Tuesday, October 28, 2014 10:02 AM
    Tuesday, October 28, 2014 10:01 AM
  • Hi Roach,

    Thanks for the reply

    The requirement was to programatically download these '*.ser' files which is placed in some server to local machine . Since the '*.ser', '*.dll' file type have some restriction on downloading, we taught of reading the files(*.ser) in the server and create the xml with the '*.ser' file name and its content and finally save that xml in local machine.

    <SerializedFiles>
         <SerializedFile>
            <FileName>File1.SER</FileName>
            <Content>^..ABCD23</Content>
          </SerializedFile>
          <SerializedFile>
             <FileName>File2.SER</FileName>
             <Content>$..XYZ234w</Content>
            </SerializedFile>
    </SerializedFiles>

    After creating the xml with required data(as explained above), we taught of reading the xml and recreate the '*.ser' file with the contents added to it. So that finally we will be having the required files at our local machine.

    I don't have the information how these files are created, i think using Java with platform dependent format, let me confirm the same.

    I tried to look into the content of 'File1.SER' by opening it in notepad, but it is not in readable format.

    Regards,

    Chetan.


    Chetan Rajakumar

    Tuesday, October 28, 2014 10:36 AM
  • Well, if you only want to add the name and the content of one .ser file to one element within the XML, then you do not have to deserialize anything.

    Just read the files and add the XML elements:

    foreach (string file in Directory.EnumerateFiles("", "*.SER"))
    			{
    				string content = File.ReadAllText( file );
    				xmlWriter.WriteStartElement("SerializedFile");
    				xmlWriter.WriteElementString("FileName", Path.GetFileName(file));
    				xmlWriter.WriteElementString("Content", content);
    				xmlWriter.WriteEndElement();
    			 }

    Tuesday, October 28, 2014 10:48 AM
  • Hi Roach,

    I tried with the same code before, I was getting the below exception when executing this line  xmlWriter.WriteElementString("Content", content);

    System.ArgumentException : {"'.', hexadecimal value 0x00, is an invalid character."}

    Regards,

    Chetan.


    Chetan Rajakumar

    Tuesday, October 28, 2014 10:55 AM
  • You might have to encode the content of the ser file:

    foreach (string file in Directory.EnumerateFiles("", "*.SER"))
    			{
    				byte[] content = File.ReadAllBytes( file );
    				xmlWriter.WriteStartElement("SerializedFile");
    				xmlWriter.WriteElementString("FileName", Path.GetFileName(file));
    				xmlWriter.WriteElementString("Content", System.Convert.ToBase64String( content ) );
    				xmlWriter.WriteEndElement();
    			 }

    Tuesday, October 28, 2014 11:02 AM
  • Thanks Roach, I had to encode the content in order to create the xml file.

    I had included only 2(*.ser) files out of 40+ files and the size of created xml file 700+ KB. I should compress the xml after creating it right?

    When i try to recreate the '*.ser' in local machine, i have to extract the compressed xml content and traverse the node and create the '*.ser' file. While creating '*.ser', do i need to decode the encode content so that i can get the exact '*.ser' file saved locally ?

    Regards,

    Chetan.


    Chetan Rajakumar

    Tuesday, October 28, 2014 11:36 AM
  • Hello Chetan,

    It seems that the replies of Roach help solve your original question, please mark it as an answer if it is helpful to you.

    If you have a new question, please open a new thread to discuss it. This can be beneficial to other community members reading the thread.

    Thanks & Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 29, 2014 2:45 AM
    Moderator