简体   繁体   English

C# ASP.NET 中的 XML 序列化问题

[英]XML serialization problem in C# ASP.NET

i keep getting this error:我不断收到此错误:

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.异常详细信息:System.NotSupportedException:无法序列化 System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 类型的成员 HannaPrintsDataAccess.Customer.CustomerAddresses,因为它是一个接口。

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.]这是堆栈跟踪: [NotSupportedException:无法序列化 System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] 类型的成员 HannaPrintsDataAccess.Customer.CustomerAddresses ] 因为它是一个接口。]

[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.] [InvalidOperationException: 无法序列化类型为 'System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 的成员 'HannaPrintsDataAccess.Customer.CustomerAddresses',有关更多详细信息,请参阅内部异常。]
System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +889917 System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +889917
System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +132 System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +132
System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo) +129 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.InitializeStructMembers(StructMapping 映射,StructModel 模型,Boolean openModel,String typeName,RecursionLimiter 限制器)+1226
System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +460 System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel 模型,String ns,Boolean openModel,XmlAttributes a,RecursionLimiter 限制器) +460
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1415 System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型,XmlAttributes a,布尔重复,布尔 openModel,RecursionLimiter 限制器)+1415

[InvalidOperationException: There was an error reflecting type 'HannaPrintsDataAccess.Customer'.] [InvalidOperationException: 有一个错误反映类型 '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.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型,XmlAttributes a,布尔重复,布尔 openModel,RecursionLimiter 限制器)+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.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.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 System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping 映射,StructModel 模型,Boolean openModel,String typeName,RecursionLimiter 限制器)+1273

[InvalidOperationException: There was an error reflecting property 'Customer'.] [InvalidOperationException:反映属性“客户”时出现错误。]
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +2167 System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping 映射,StructModel 模型,布尔型 openModel,String typeName,RecursionLimiter 限制器)+2167
System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +460 System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel 模型,String ns,Boolean openModel,XmlAttributes a,RecursionLimiter 限制器) +460
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1415 System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型,XmlAttributes a,布尔重复,布尔 openModel,RecursionLimiter 限制器)+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 [InvalidOperationException:有一个错误反映类型'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.CreateArrayElementsFromAttributes(ArrayMapping arrayMapping,XmlArrayItemAttributes 属性,类型arrayElementType,字符串arrayElementNs,RecursionLimiter 限制器)+227
System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel model, String ns, RecursionLimiter limiter) +207 System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel 模型,字符串 ns,RecursionLimiter 限制器)+207
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1143 System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型,XmlAttributes a,布尔重复,布尔 openModel,RecursionLimiter 限制器)+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 [InvalidOperationException: 有一个错误反映类型 'HannaPrintsDataAccess.CustomerAddress[]'。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter限制器)+1621
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter) +28 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.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.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, String defaultNamespace) +461
System.Xml.Serialization.XmlSerializer..ctor(Type type) +6 System.Xml.Serialization.XmlSerializer..ctor(类型类型)+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 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.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 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.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

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!!!谢谢!!!

UPDATE I tried changing the IList to List, but it does not do anything.更新我尝试将 IList 更改为 List,但它没有做任何事情。 Here is my classes with the customer address:这是我与客户地址的课程:

CUSTOMER CLASS:::客户类别:::

using System;

using System.Collections.Generic;使用 System.Collections.Generic; using Castle.ActiveRecord;使用 Castle.ActiveRecord;

namespace HannaPrintsDataAccess { public partial class Customer { private IList _customerAddresses;命名空间 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;
        }
    }
}

} }

Here is the Customer Address class::::这是客户地址类::::

using System.IO;

using System.Xml.Serialization;使用 System.Xml.Serialization; using Castle.ActiveRecord;使用 Castle.ActiveRecord;

namespace HannaPrintsDataAccess { public partial class CustomerAddress { public string ToXml() { XmlSerializer serializer = new XmlSerializer(GetType()); namespace HannaPrintsDataAccess { public partial class CustomerAddress { public string ToXml() { XmlSerializer serializer = new XmlSerializer(GetType()); MemoryStream memoryStream = new MemoryStream(); MemoryStream memoryStream = new MemoryStream(); serializer.Serialize(memoryStream, this); serializer.Serialize(memoryStream, this); memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.Seek(0, SeekOrigin.Begin); return new StreamReader(memoryStream).ReadToEnd();返回新的 StreamReader(memoryStream).ReadToEnd(); } }

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

} }

Thanks again for your help再次感谢您的帮助

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>假设该成员实际上是List<T>一个实例

Basically, this attribute is required to support inheritance with Xml serialization.基本上,需要此属性来支持 Xml 序列化的继承。 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>.也就是说,如果属性是 IList 类型并且实例是List<T>类型(继承自 IList),则需要将该属性添加到指定List<T>.的属性List<T>.

As the error message explicitly tells you the XmlSerializer cannot serialize interfaces.由于错误消息明确告诉您XmlSerializer无法序列化接口。

You probably had:你可能有:

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

which you changed to:您更改为:

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

which no longer works.这不再有效。

The interface property is still public , so the serializer will still attempt to serialize it (and crash).接口属性仍然是public ,所以序列化器仍然会尝试序列化它(并崩溃)。

Put an XmlIgnore attribute on the interface property like this:在接口属性上放置一个XmlIgnore属性,如下所示:

[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.List实现也搞砸了。 Add this declaration right below your IList<CustomerAddress> property declaration:在您的IList<CustomerAddress>属性声明下方添加此声明:

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

HTH...哈...

PS For future reference, remember that all public properties are visible to the xml serializer, and it will try to serialize them. PS 为了将来参考,请记住所有公共属性对 xml 序列化程序都是可见的,并且它尝试序列化它们。 Make sure to put XmlIgnore attributes on the properties that should not be serialized.确保将XmlIgnore特性放在不应序列化的属性上。

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.异常详细信息:System.NotSupportedException:无法序列化 System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 类型的成员 HannaPrintsDataAccess.Customer.CustomerAddresses,因为它是一个接口。

Try replacing the type of HannaPrintsDataAccess.Customer.CustomerAddresses , which is currently IList<CustomerAddress> , with the concrete type (eg List<CustomerAddress> ) instead of the interface.尝试将HannaPrintsDataAccess.Customer.CustomerAddresses的类型(当前为IList<CustomerAddress> )替换为具体类型(例如List<CustomerAddress> )而不是接口。

我对 $.ajax 请求有这样的例外, contentType: "application/json; charset=utf-8"解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM