2010-09-02 64 views
0

我不断收到此错误:XML序列化问题

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.

Here is the stack trace: [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.]

[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.]
System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +889917
System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +132
System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo) +129
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +1226
System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +460
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1415

[InvalidOperationException: There was an error reflecting type 'HannaPrintsDataAccess.Customer'.]
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1621
System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +10296
System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter) +139
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +1273

[InvalidOperationException: There was an error reflecting property 'Customer'.]
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +2167
System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +460
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1415

[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
System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, String arrayElementNs, RecursionLimiter limiter) +227
System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel model, String ns, RecursionLimiter limiter) +207
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1143

[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
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter) +28
System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter) +125
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) +97
System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) +461
System.Xml.Serialization.XmlSerializer..ctor(Type type) +6
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
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

我似乎无法弄清楚这一点,我敢肯定它只是一些配置,需要我们拿到的这款之前因为改变代码,据说它工作正常,但我们不知道为什么我们得到这个错误。如果任何人都可以提供帮助,那会很棒。谢谢!!!

UPDATE 我试图改变IList中列出,但它不会做任何事情。这里是我的类与客户地址:

Customer类:::使用System.Collections.Generic

using System; 

; 使用Castle.ActiveRecord;

命名空间HannaPrintsDataAccess { 公共部分类客户 { 私人的IList _customerAddresses;

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<CustomerAddress> CustomerAddresses 
    { 
     get 
     { 
      return this._customerAddresses; 
     } 
     set 
     { 
      this._customerAddresses = value; 
     } 
    } 
} 

}

下面是使用的System.Xml.Serialization客户地址类::::

using System.IO; 

; 使用Castle.ActiveRecord;

命名空间HannaPrintsDataAccess { 公共部分类CustomerAddress { 公共toxml用于字符串() { XmlSerializer的串行=新的XmlSerializer(的GetType()); MemoryStream memoryStream = new MemoryStream(); serializer.Serialize(memoryStream,this); memoryStream.Seek(0,SeekOrigin.Begin); 返回新的StreamReader(memoryStream).ReadToEnd(); }

[BelongsTo("CustomerId")] 
    public virtual Customer Customer { get; set; } 
} 

}再次

感谢您的帮助

回答

1

由于错误消息明确告诉您XmlSerializer无法序列化接口。

你可能有:

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

您更改为:

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

不再起作用。

+0

嘿,我试了这个,它没有工作,我更新了我的问题,两个类的代码 – anthonypliu 2010-09-02 18:55:35

1

你可以把

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

的财产

HannaPrintsDataAccess 

假设日e成员实际上是一个实例List<T>

基本上,该属性需要支持Xml序列化继承。也就是说,如果一个属性的类型是IList的,实例是List<T>类型(自IList继承)的,则需要该属性添加到属性指定List<T>.

+0

您不能使用'XmlInclude'属性修饰属性。 – 2010-09-02 18:39:32

+0

认识到并纠正了。 – Jeff 2010-09-02 18:43:03

+0

这也不起作用。 – 2010-09-02 18:47:28

0

该错误消息似乎非常清楚对我说:

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.

尝试更换的HannaPrintsDataAccess.Customer.CustomerAddresses的类型,这是目前IList<CustomerAddress>,与混凝土类型(例如List<CustomerAddress>)代替的接口。

+0

嗨,我试过这个解决方案,但它没有工作,我做了一个编辑我的问题与更多信息 – anthonypliu 2010-09-02 19:00:25

1

接口属性仍然是public,所以串行器仍然会尝试序列化它(和崩溃)。

放在这样的接口属性的XmlIgnore属性:

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

List实现也搞砸了。添加这个声明下方您IList<CustomerAddress>财产申报:

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

HTH ...

附:为了将来的参考,请记住所有公共属性对于xml序列化程序都是可见的,并且尝试序列化它们。确保将XmlIgnore属性放在不应该序列化的属性上。

+0

嘿,我试了这个,它仍然给我相同的确切的错误,我不不明白为什么,因为我仔细检查了我的远程文件以确保所做的更改通过了,但它仍然有相同的错误消息,即使我将其更改为List而不是IList。 – anthonypliu 2010-09-02 19:31:46