views:

1522

answers:

1

Hi there,

Can anyone help?

I have a method for converting an object to a dataset. In this case the object is a collection (using LIST) of a a collection class

Here is the code but it errors with the following error on the line XmlSerializer formatter= ...

This is the error

There was an error reflecting type 'System.Collections.Generic.List`1[MyNameSpace.Model.InformeVehiculo]'.

    public static DataSet TransformObjectToDataSet(System.Type type, object datos, string nombre)
    {
        XmlSerializer formatter = new XmlSerializer(type, "Coleccion" + nombre);
        MemoryStream buffer = new MemoryStream();
        formatter.Serialize(buffer, datos);
        buffer.Position = 0;
        DataSet dtsDatos = new DataSet(nombre);
        dtsDatos.ReadXml(buffer);

        return dtsDatos;
    }

I am calling the method like so

   TransformObjectToDataSet(typeof(List<InformeVehiculo>), objColeccionInformeVehiculo, "ColeccionInformeVehiculo");

objColeccionInformeVehiculo is a List

I am a little lost, does anyone know why its failing

EDIT

After looking further down the InnerExeption, the last one displays the following

 {"Cannot serialize member MyNameSpace.Model.ObjectChangeTracker.ObjectsRemovedFromCollectionProperties of type MyNameSpace.Model.ObjectsRemovedFromCollectionProperties, because it implements IDictionary."}

Actually inside the InformeVehiculo i have extended it to hold a reference to InformeContracto (using partial classes, it is a model originally created by entity framework)

InformeContrato is where its failing.

This my class

 public partial class InformeContrato : IObjectWithChangeTracker, INotifyPropertyChanged

InformeContrato did NOT exist unlike InformeVehiculo which existed as a partial class created by entity framework. So I created InformeContrato manually and inserted the changeTracking stuff..

Further down I have a region which i copied from a MODEL created by Entity Framework.

    #region ChangeTracking

    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
        {
            ChangeTracker.State = ObjectState.Modified;
        }
        if (_propertyChanged != null)
        {
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected virtual void OnNavigationPropertyChanged(String propertyName)
    {
        if (_propertyChanged != null)
        {
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { add { _propertyChanged += value; } remove { _propertyChanged -= value; } }
    private event PropertyChangedEventHandler _propertyChanged;
    private ObjectChangeTracker _changeTracker;

    //[DataMember]
    public ObjectChangeTracker ChangeTracker
    {
        get
        {
            if (_changeTracker == null)
            {
                _changeTracker = new ObjectChangeTracker();
                _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
            }
            return _changeTracker;
        }
        set
        {
            if (_changeTracker != null)
            {
                _changeTracker.ObjectStateChanging -= HandleObjectStateChanging;
            }
            _changeTracker = value;
            if (_changeTracker != null)
            {
                _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
            }
        }
    }

    private void HandleObjectStateChanging(object sender, ObjectStateChangingEventArgs e)
    {
        if (e.NewState == ObjectState.Deleted)
        {
            ClearNavigationProperties();
        }
    }

    protected bool IsDeserializing { get; private set; }

    [OnDeserializing]
    public void OnDeserializingMethod(StreamingContext context)
    {
        IsDeserializing = true;
    }

    [OnDeserialized]
    public void OnDeserializedMethod(StreamingContext context)
    {
        IsDeserializing = false;
        ChangeTracker.ChangeTrackingEnabled = true;
    }

    protected virtual void ClearNavigationProperties()
    {
        //AccesorioContrato.Clear();
    }

    #endregion
A: 

IDictionary is not serializable. Nor are the standard dictionaries. You will need to implement a custom dictionary type to enable serialization. Then it will work.

EX for XML Serialization:

  [XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue>
    : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof (TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");

            reader.ReadStartElement("key");
            TKey key = (TKey) keySerializer.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            TValue value = (TValue) valueSerializer.Deserialize(reader);
            reader.ReadEndElement();

            Add(key, value);

            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof (TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue));

        foreach (TKey key in Keys)
        {
            writer.WriteStartElement("item");

            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();

            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }

    #endregion 
}
Jennifer Zouak
Thanks Jennifer, ok Is there a Attribute I can place above the ChangeObject in InformeContrato [NonSerializable], will this work?
mark smith
Yes. XmlIgnoreAttribute. You must put the attribute on the instance (i.e. where you say you "hold a reference"), not on the Class itself:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlignoreattribute.aspx
Jennifer Zouak