1

I've been searching on google about this for an hour but I think I don't use the right word because I can't find a very simple example of what I'm trying to do. People always use complexe structure like List or derived object in the samples.

All I want to do is to XMLSerialize my main object called SuperFile to a file. This SuperFile class contains 2 members and these 2 members are not serialized so the resulting XML file is empty (containing only the header).

Here is my code, what am I doing wrong?

SuperFile

public class SuperFile
{
        private NetworkInfo _networkInfo;
        private Planification _planification;

        public NetworkInfo NI
        {
            get
            {
                return _networkInfo;
            }
        }

        public Planification Planif
        {
            get
            {
                return _planification;
            }
        }
}

NetworkInfo and Planification are very normal class with mostly double member and they serialize perfectly on their own if I want. But now, I want them to serialize inside the SuperFile object.

Finally, here is my code to do the serialization

public void Save(string strFilename)
{
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(ExoFile));
    TextWriter WriteFileStream = new StreamWriter(strFilename);
    x.Serialize(WriteFileStream, this);

    WriteFileStream.Close();
}

If I put this inside SuperFile, it get serialized but the 2 other member gets skipped. I think it get serialize since it's not a complex type...

public int _nDummy;

Hope it's clear!

Thanks!

Jean-François Côté
  • 4,200
  • 11
  • 52
  • 88

1 Answers1

2

XMLSerializer has some limitations, one of which is to require a setter. (It also doesn't serialise private fields, indexers..). It's not an obvious gotcha, and has had me scratching my head in the past :)

here's an answer with some details - Why isn't my public property serialized by the XmlSerializer?

Community
  • 1
  • 1
NDJ
  • 5,189
  • 1
  • 18
  • 27