0

I'm trying to serialize a custom object into a file. i've tried many thing but no one works, i must have missed a thing.

Here's the problem.

I have a singleton class i'm using to store my objects. Here is the code :

using DataLibrary.Model.Tests;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using testsPsychotechniques.View;

namespace testsPsychotechniques.Model
{
    [Serializable()]
    public class testSaver : ISerializable
    {
        private String lastName;
        private String firstName;
        private List<ITest> tests;


        public static testSaver Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        public void addTest(ITest test)
        {
            tests.Add(test);
        }

        public Boolean save()
        {
            try
            {
                FileStream file = File.Open(".\\result.data",
                                            FileMode.Create,
                                            FileAccess.ReadWrite,
                                            FileShare.None);

                XmlSerializer serializer = new XmlSerializer(typeof(testSaver));
                serializer.Serialize(file, testSaver.Instance);
                file.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        private testSaver()
        {
            this.firstName = Identification.firstName;
            this.lastName = Identification.lastName;
            this.tests = new List<ITest>();
        }


        private class Nested
        {
            internal static readonly testSaver instance = new testSaver();
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("lastName", this.lastName);
            info.AddValue("firstName", this.firstName);
            info.AddValue("testsResults", this.tests);
        }
    }
}

Now the real problem :

When i'm calling the save method, in the xml file generated i only have this data :

<?xml version="1.0"?>
<testSaver xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

The classes implementing the ITest interface are all marked serializable and have a method getObjectData.

Another hint is that the functions getObjectData are never used.

Thanks for any help

Irwene
  • 2,807
  • 23
  • 48

1 Answers1

1

XmlSerializer does not use the [Serializable] attribute or the ISerializable interface.

You have two options here:

  1. Continue to use XmlSerializer in which case you need public properties and a public noarg constructor.
  2. Change XmlSerializer to SoapFormatter and continue to use either [Serializable] or ISerializable, you don't need both.

It really depends what format you want your Xml in and whether you prefer attributes or code to control the serialization. I would go for option 1 as it is generally simpler.

Iain
  • 2,500
  • 1
  • 20
  • 24
  • Ok so my problem here was that my properties were not public ? What if i changed from the XMLSerializer to the BinarySerializer ? Same problem ? In any case i'll give it a try – Irwene Dec 30 '13 at 09:42
  • 1
    Yes exactly. Yes if binary is fine then you could indeed switch to the `BinaryFormatter` – Iain Dec 30 '13 at 09:48
  • Ok i used the xml one just to check the file content. – Irwene Dec 30 '13 at 09:57
  • Seems only the `lastName` and `firstname` are saved in the binary file. Do I need to do something specific for the classes implementing the ITest interface ? – Irwene Dec 30 '13 at 10:09
  • Found, i just missed to add the `[Serialize]` tag on the aggregated classes. Oh and the SoapFOrmatter is having the same problem than the XmlSerializer (i mean for it to perform correctly the object must have public members), the `BinaryFormatter` doesn't seems to have the limitation. – Irwene Dec 30 '13 at 10:33