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!

2
  • try putting a setter on the properties (NI and Planif); Commented Sep 30, 2013 at 23:22
  • Thats was it! Thank you NDJ. Can you write a complete answer and explain why? Commented Sep 30, 2013 at 23:25

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.