2

I am trying to serialize an object into the database using xml serialization, however when deserializing it I am getting an error.

The error is There is an error in XML document (2, 2) with an inner exception of "<MyCustomClass xmlns=''> was not expected."

The code I am using to serialize is:

public static string SerializeToXml<T>(T obj)
{
    if (obj == null)
        return string.Empty;

    StringWriter xmlWriter = new StringWriter();
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    xmlSerializer.Serialize(xmlWriter, obj);
    return xmlWriter.ToString();
}

public static T DeserializeFromXml<T>(string xml)
{
    if (xml == string.Empty)
        return default(T);

    T obj;
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    StringReader xmlReader = new StringReader(xml);
    obj = (T)xmlSerializer.Deserialize(xmlReader);
    return obj;
}

The SerializedXml begins with:

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

This is my first time using serialization and I'm wondering what I'm doing something wrong with my code.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • As an aside, normally `xmlReader` and `xmlWriter` would be used for instances of `XmlReader` and `XmlWriter`, not `StringReader` and `StringWriter`. – Steven Sudit Aug 18 '10 at 19:59
  • To solve this, I think we'll need to see the declaration of `MyCustomClass`, as well as the rest of the XML. – Steven Sudit Aug 18 '10 at 19:59
  • It is telling me I can't create an instance of the abstract class XmlWriter – Rachel Aug 18 '10 at 20:01
  • My equivalent code works good. Can you serialize empty class? Then add some properties to it. – alxx Aug 18 '10 at 20:07
  • The MyCustomClass code is fairly long... it contains no attributes other then `[Serializable]` and its only property is a CustomObservableCollection (inherited from base ObservableCollection) of CustomObjects. – Rachel Aug 18 '10 at 20:07
  • @Rachel: please don't put tags like "C#" in your title. It's enough to have them in your tags. – John Saunders Aug 18 '10 at 20:08
  • Would I be correct in thinking your XML does't actually say but rather since the quotes aren't supposed to be escaped like that in the XML but if you copied it out of a debugger window it would be. If so will you or someone edit it for clarity? – Peter T. LaComb Jr. Aug 18 '10 at 20:14
  • Yes you are correct, I copied that from the debugger – Rachel Aug 19 '10 at 11:36

3 Answers3

1

BTW, you need using blocks around your code:

using (StringReader reader = new StringReader(xml))
{
    obj = (T)xmlSerializer.Deserialize(reader);
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
1

Unfortunately, XmlSerialization exceptions is a piece of crap.

You normally have to drill down into countless levels of inner exceptions to get to the real error.

leppie
  • 115,091
  • 17
  • 196
  • 297
1

I'm sorry, I just realized my problem was stupidity =/

I was serializing the class but trying to deserialize the ObservableCollection only. Once I changed that to serializing/deserialing the correct object it works great, although I thank you for the tip about using blocks

Rachel
  • 130,264
  • 66
  • 304
  • 490