c# xml serialize a class - if I change the class I can't deserialize file how can I fix?

I have two static functions below which I use to serialize and deserialize classes into files.  So for example, I do some things to a class on a form, save it to a file, then when I open the file, I rehydrate it.  If I change the class I can't open it again because the xml doesn't match.  Is there a way I can add properties to the class and be able to rehydrate using my old xml?  I don't mind setting default values for any new variables.  For example, if I want to create an IsActive boolean property set to false by default, I'd like to be able to do that without having to recreate the xml.






        public static void SerializeObjectToFile<T>(this T source, string path)
        {
            //XmlModelController.XmlSerializeToFile<T>(path, source);
            string serializedModel = String.Empty;

            var stringWriter = new StringWriter();
            try
            {
                new XmlSerializer(typeof(T)).Serialize(stringWriter, source);
                serializedModel = stringWriter.ToString();
            }
            finally
            {
                stringWriter.Dispose();
            }

            File.WriteAllText(path, serializedModel);
        }

        //this new thing on the end allows me to new up a T, if I didn't have it I would pass back a null object
        public static T XmlDeSerializeFile<T>(this T source, string filePath) where T : new()
        {
            //if it doesn't exist return a blank one
            //T returnValue = default(T);
            T returnValue = new T();
            if (File.Exists(filePath))
            {
                var reader = new StreamReader(filePath);
                try
                {
                    returnValue = (T)new XmlSerializer(typeof(T)).Deserialize(reader);
                }
                finally
                {
                    reader.Dispose();
                }
            }
            return returnValue;
        }
jackjohnson44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
The code generated for the new class does not match the format of the file.

Serialization has been designed as a way to easily format information for transmission, not for file saving as you have found.

For data transmission, since the serialization and deserialization occur with very little time between both, all you have to do is to make sure that both sides of the communications are updated when a change is made in the class.

In my case, the easiest way I have found to work around your problem is to keep the old class, deserialize into it, and then copy the retrieved values into the new class. And I stopped using serialization for files afterward, going back to old binary files. More code to write, but I have full control and found that it is faster.

But I found out, without ever using it, that there is a mechanism in the framework to let you control the serialization and thus have an application that can work with 2 versions of the same class. It's all explained at http://msdn.microsoft.com/en-us/library/ty01x675.aspx.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.