tags:

views:

117

answers:

4
A: 

You can put

[System.Xml.Serialization.XmlElement(Type = typeof(List<HannaPrintsDataAccess.CustomerAddress>) )]

on the property

HannaPrintsDataAccess

Assuming that the member is in fact an instance of List<T>

Basically, this attribute is required to support inheritance with Xml serialization. That is, if a property is of type IList and the instance is of type List<T> (which inherits from IList), you will need to add that attribute to the property specifying List<T>.

JeffN825
You cannot decorate a property with the `XmlInclude` attribute.
Darin Dimitrov
Realized that and corrected.
JeffN825
This doesn't work neither.
Darin Dimitrov
Can you please explain why not?
JeffN825
"It is important to note here that relying on inheritance is not sufficient. Developers must declare derived types to the XmlSerializer, either by attaching XmlInclude attributes to the base class or by attaching XmlElement attributes to the fields that can hold objects of types derived from the declared type." @ http://msdn.microsoft.com/en-us/library/aa302290.aspx
JeffN825
Hi, I tried this solution but it did not work, I made an edit in my question with more info
anthonypliu
The problem is with the property CustomerAddress on class Customer. What is the actual instance type of CustomerAddress on Customer? That is, if you stop at a breakpoint and type myCustomer.CustomerAddress.GetType().FullName, what prints out? It's this type that you need to put in the attribute on the CustomerAddress property.
JeffN825
See also http://stackoverflow.com/questions/753099/how-to-use-xmlserializer-with-a-castle-activerecord-containing-an-ilistt-member
JeffN825
A: 

The error message seems pretty clear to me:

Exception Details: System.NotSupportedException: Cannot serialize member HannaPrintsDataAccess.Customer.CustomerAddresses of type System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.

Try replacing the type of HannaPrintsDataAccess.Customer.CustomerAddresses, which is currently IList<CustomerAddress>, with the concrete type (e.g. List<CustomerAddress>) instead of the interface.

Heinzi
Hi, I tried this solution but it did not work, I made an edit in my question with more info
anthonypliu
+1  A: 

As the error message explicitly tells you the XmlSerializer cannot serialize interfaces.

You probably had:

public List<CustomerAddress> CustomerAddresses  { get; set; }

which you changed to:

public IList<CustomerAddress> CustomerAddresses  { get; set; }

which no longer works.

Darin Dimitrov
Hey i tried this and it did not work, Ive updated my question with the code from both classes
anthonypliu
A: 

The interface property is still public, so the serializer will still attempt to serialize it (and crash).

Put an XmlIgnore attribute on the interface property like this:

[XmlIgnore]
[HasMany(typeof(CustomerAddress), ColumnKey = "CustomerId", Table = "Customer")]
public virtual IList<CustomerAddress> CustomerAddresses
{
    get
    {
        return this._customerAddresses;
    }
    set
    {
        this._customerAddresses = value;
    }
}

You List implementation is screwed up also. Add this declaration right below your IList<CustomerAddress> property declaration:

public List<CustomerAddress> CustomerAddresses
{
    get
    {
        return (List<CustomerAddress>)this._customerAddresses;
    }
    set
    {
        this._customerAddresses = value;
    }
}

HTH...

P.S. For future reference, remember that all public properties are visible to the xml serializer, and it will try to serialize them. Make sure to put XmlIgnore attributes on the properties that should not be serialized.

code4life
Hey, i tried this and it still gives me the SAME exact error, I don't understand why, because I double checked my remote files to make sure the changes went through, but it still has the same error message, even if i change it to List instead of IList.
anthonypliu