Shouldn’t this work?

November 6, 2008

SerializationError

 

Well it doesn’t… I get this error: “Root element is missing.”

 
Code attached:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace SerializationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SerializeThis));
            Stream stream = new MemoryStream();  

            SerializeThis st = new SerializeThis();
            st.Name = "Name123Test";

            serializer.Serialize(stream, st);

            SerializeThis stDeserialized =
                (SerializeThis)serializer.Deserialize(stream);

            Console.WriteLine(stDeserialized.Equals(st));
        }
    }

    public class SerializeThis
    {
        public string Name;

        public override bool Equals(object obj)
        {
            return ((SerializeThis)obj).Name == this.Name;
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: