title: XML serialization problem in C# ASP.NET
body: <p>i keep getting this error:</p>
  
  <blockquote>
    <p>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.</p>
    
    <p><strong>Here is the stack trace:</strong> [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.]</p>
    
    <p>[InvalidOperationException: 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]]', see inner
    exception for more details.]<br>
    System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc
    typeDesc, MemberInfo member, Type
    type) +889917<br>
    System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo
    propertyInfo) +132<br>
    System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo
    memberInfo) +129<br>
    System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping
    mapping, StructModel model, Boolean
    openModel, String typeName,
    RecursionLimiter limiter) +1226<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel
    model, String ns, Boolean openModel,
    XmlAttributes a, RecursionLimiter
    limiter) +460<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
    model, String ns, ImportContext
    context, String dataType,
    XmlAttributes a, Boolean repeats,
    Boolean openModel, RecursionLimiter
    limiter) +1415</p>
    
    <p>[InvalidOperationException: There was
    an error reflecting type
    'HannaPrintsDataAccess.Customer'.]<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
    model, String ns, ImportContext
    context, String dataType,
    XmlAttributes a, Boolean repeats,
    Boolean openModel, RecursionLimiter
    limiter) +1621<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping
    accessor, FieldModel model,
    XmlAttributes a, String ns, Type
    choiceIdentifierType, Boolean rpc,
    Boolean openModel, RecursionLimiter
    limiter) +10296<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel
    parent, FieldModel model,
    XmlAttributes a, String ns,
    RecursionLimiter limiter) +139<br>
    System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping
    mapping, StructModel model, Boolean
    openModel, String typeName,
    RecursionLimiter limiter) +1273</p>
    
    <p>[InvalidOperationException: There was
    an error reflecting property
    'Customer'.]<br>
    System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping
    mapping, StructModel model, Boolean
    openModel, String typeName,
    RecursionLimiter limiter) +2167<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel
    model, String ns, Boolean openModel,
    XmlAttributes a, RecursionLimiter
    limiter) +460<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
    model, String ns, ImportContext
    context, String dataType,
    XmlAttributes a, Boolean repeats,
    Boolean openModel, RecursionLimiter
    limiter) +1415</p>
    
    <p>[InvalidOperationException: There was
    an error reflecting type
    'HannaPrintsDataAccess.CustomerAddress'.]
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
    model, String ns, ImportContext
    context, String dataType,
    XmlAttributes a, Boolean repeats,
    Boolean openModel, RecursionLimiter
    limiter) +1621<br>
    System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping
    arrayMapping, XmlArrayItemAttributes
    attributes, Type arrayElementType,
    String arrayElementNs,
    RecursionLimiter limiter) +227<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel
    model, String ns, RecursionLimiter
    limiter) +207<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
    model, String ns, ImportContext
    context, String dataType,
    XmlAttributes a, Boolean repeats,
    Boolean openModel, RecursionLimiter
    limiter) +1143</p>
    
    <p>[InvalidOperationException: There was
    an error reflecting type
    'HannaPrintsDataAccess.CustomerAddress[]'.]
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
    model, String ns, ImportContext
    context, String dataType,
    XmlAttributes a, Boolean repeats,
    Boolean openModel, RecursionLimiter
    limiter) +1621<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
    model, String ns, ImportContext
    context, String dataType,
    XmlAttributes a, RecursionLimiter
    limiter) +28<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel
    model, XmlRootAttribute root, String
    defaultNamespace, RecursionLimiter
    limiter) +125<br>
    System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type
    type, XmlRootAttribute root, String
    defaultNamespace) +97<br>
    System.Xml.Serialization.XmlSerializer..ctor(Type
    type, String defaultNamespace) +461<br>
    System.Xml.Serialization.XmlSerializer..ctor(Type
    type) +6<br>
    HannaPrintsBusiness.Services.Live.CustomerService.GetCustomerAddressesXml(CustomerAddress[]
    customerAddresses) in
    E:\ProjectsOld\HannaPrints\HannaPrintsBusiness\Services\Live\CustomerService.cs:148
    CreateGreetingCard.orderButton_Click(Object
    sender, EventArgs e) in
    c:\HostingSpaces\greetwus\galadavetiye.com\wwwroot\HannaPrints\HannaPrints\WebUI\CreateGreetingCard.aspx.cs:198
    System.Web.UI.WebControls.Button.OnClick(EventArgs
    e) +111<br>
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String
    eventArgument) +110<br>
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    eventArgument) +10<br>
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument)
    +13    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
    postData) +36<br>
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean
    includeStagesAfterAsyncPoint) +1565</p>
  </blockquote>
  
  <p>I cant seem to figure this out, I am pretty sure its just some configuration that needs to be changed because before we got this code, supposedly it was working correctly, but we cant figure out why we are getting this error.  If anyone could help that would be great.  Thanks!!!</p>
  
  <p><strong><em>UPDATE</em></strong> 
  I tried changing the IList to List, but it does not do anything.  Here is my classes with the customer address:</p>
  
  <p>CUSTOMER CLASS:::</p>
  
  <pre><code>using System;
  </code></pre>
  
  <p>using System.Collections.Generic;
  using Castle.ActiveRecord;</p>
  
  <p>namespace HannaPrintsDataAccess
  {
      public partial class Customer
      {
          private IList _customerAddresses;</p>
  
  <pre><code>    public CustomerAddress GetPrimaryCustomerAddress()
      {
          foreach (CustomerAddress address in _customerAddresses)
          {
              if (address.IsPrimary)
                  return address;
          }
          return null;
      }
  
  
      [HasMany(typeof(CustomerAddress), ColumnKey = "CustomerId", Table = "Customer")]
      public virtual IList&lt;CustomerAddress&gt; CustomerAddresses
      {
          get
          {
              return this._customerAddresses;
          }
          set
          {
              this._customerAddresses = value;
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the Customer Address class::::</p>
  
  <pre><code>using System.IO;
  </code></pre>
  
  <p>using System.Xml.Serialization;
  using Castle.ActiveRecord;</p>
  
  <p>namespace HannaPrintsDataAccess
  {
      public partial class CustomerAddress
      {
          public string ToXml()
          {
              XmlSerializer serializer = new XmlSerializer(GetType());
              MemoryStream memoryStream = new MemoryStream();
              serializer.Serialize(memoryStream, this);
              memoryStream.Seek(0, SeekOrigin.Begin);
              return new StreamReader(memoryStream).ReadToEnd();
          }</p>
  
  <pre><code>    [BelongsTo("CustomerId")]
      public virtual Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Thanks again for your help</p>