views:

43

answers:

1

I am trying to serialize and deserialize objects in silverlight, but cannot seem to get it to work. Serializing works just fine but it complains about my rootObject not being expected. I don't get it because it it is Silverlight who generated the XML.

Btw; i'm still new to C#

I have a class which looks like:

[XmlRoot("DataStorage")] // has no effect
public class DataStorage
{
    public string type { get; set; }
    public string imgUrl { get; set; }
    public List<AbstractionObject> children { get; set; }

    public DataStorage()
    {
        type = "default";
        children = new List<AbstractionObject>();
    }
}

When serialized by Silverlight it produces:

<?xml version="1.0" encoding="utf-8"?>
<DataStorage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <type>default</type>
  <children />
</DataStorage>

However, when I attempt to deserialize it produces:

System.InvalidOperationException was unhandled by user code Message=There is an error in XML document (2, 2).

InnerException: System.InvalidOperationException Message=<DataStorage xmlns=''> was not expected.

My deserialization code is as follows:

// create the xmlSerializer for DataObject
XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataObject));

// Open the file again for reading.
StreamReader fileStream = new StreamReader(isoStore.OpenFile("IsoStoreFile.xml", FileMode.Open));
XmlReader xmlReader = XmlReader.Create(fileStream);

DataObject deserializedObject = (DataObject)xmlSerializer.Deserialize(xmlReader);
fileStream.Close();

Debug.WriteLine(deserializedObject);

Any idea?

A: 

I don't know if this may just be a typo in your example, but the class you've defined in the first block is "DataStorage" and you're serializing and deserializing a "DataObject" which may explains why it's breaking when it encounters "DataStorage".

Doobi