【问题标题】:XML serialization problem in C# ASP.NETC# ASP.NET 中的 XML 序列化问题
【发布时间】:2020-01-21 00:00:28
【问题描述】:

我不断收到此错误:

异常详情: System.NotSupportedException:不能 序列化成员 HannaPrintsDataAccess.Customer.CustomerAddresses 类型 System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, 版本=1.0.0.0,文化=中性, PublicKeyToken=null]] 因为它是 界面。

这是堆栈跟踪: [NotSupportedException: 不能 序列化成员 HannaPrintsDataAccess.Customer.CustomerAddresses 类型 System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, 版本=1.0.0.0,文化=中性, PublicKeyToken=null]] 因为它是 界面。]

[InvalidOperationException:不能 序列化成员 'HannaPrintsDataAccess.Customer.CustomerAddresses' 类型 'System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, 版本=1.0.0.0,文化=中性, PublicKeyToken=null]]',见内部 更多细节例外。]
System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc,MemberInfo 成员,类型 类型)+889917
System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo 物业信息)+132
System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo 会员信息)+129
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(结构映射 映射,StructModel 模型,布尔值 openModel,字符串类型名称, RecursionLimiter 限制器)+1226
System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel 模型,字符串 ns,布尔型 openModel, XmlAttributes a, RecursionLimiter 限制器)+460
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型, XmlAttributes a,布尔重复, 布尔型 openModel、RecursionLimiter 限制器)+1415

[InvalidOperationException:有 错误反映类型 'HannaPrintsDataAccess.Customer'。]
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型, XmlAttributes a,布尔重复, 布尔型 openModel、RecursionLimiter 限制器)+1621
System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(成员映射 访问器,FieldModel 模型, XmlAttributes a, String ns, Type 选择标识符类型,布尔 rpc, 布尔型 openModel、RecursionLimiter 限制器)+10296
System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel 父级,FieldModel 模型, XmlAttributes a, String ns, RecursionLimiter 限制器)+139
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(结构映射 映射,StructModel 模型,布尔值 openModel,字符串类型名称, RecursionLimiter 限制器)+1273

[InvalidOperationException:有 反映属性的错误 '客户'。]
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(结构映射 映射,StructModel 模型,布尔值 openModel,字符串类型名称, RecursionLimiter 限制器)+2167
System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel 模型,字符串 ns,布尔型 openModel, XmlAttributes a, RecursionLimiter 限制器)+460
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型, XmlAttributes a,布尔重复, 布尔型 openModel、RecursionLimiter 限制器)+1415

[InvalidOperationException:有 错误反映类型 'HannaPrintsDataAccess.CustomerAddress'。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型, XmlAttributes a,布尔重复, 布尔型 openModel、RecursionLimiter 限制器)+1621
System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping 数组映射,XmlArrayItemAttributes 属性,类型数组元素类型, 字符串 arrayElementNs, RecursionLimiter 限制器)+227
System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel 模型,字符串 ns,RecursionLimiter 限制器)+207
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型, XmlAttributes a,布尔重复, 布尔型 openModel、RecursionLimiter 限制器)+1143

[InvalidOperationException:有 错误反映类型 'HannaPrintsDataAccess.CustomerAddress[]'。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型, XmlAttributes a,布尔重复, 布尔型 openModel、RecursionLimiter 限制器)+1621
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型,字符串 ns,ImportContext 上下文,字符串数据类型, XmlAttributes a, RecursionLimiter 限制器)+28
System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel 模型,XmlRootAttribute 根,字符串 默认命名空间,递归限制器 限制器)+125
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(类型 类型,XmlRootAttribute 根,字符串 默认命名空间)+97
System.Xml.Serialization.XmlSerializer..ctor(类型 类型,字符串 defaultNamespace) +461
System.Xml.Serialization.XmlSerializer..ctor(类型 类型)+6
HannaPrintsBusiness.Services.Live.CustomerService.GetCustomerAddressesXml(CustomerAddress[] 客户地址)在 E:\ProjectsOld\HannaPrints\HannaPrintsBusiness\Services\Live\CustomerService.cs:148 CreateGreetingCard.orderButton_Click(对象 发件人,EventArgs e) 在 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(字符串 事件参数)+110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 源控件,字符串事件参数) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint) +1565

我似乎无法弄清楚这一点,我很确定它只是一些需要更改的配置,因为在我们获得此代码之前,据说它工作正常,但我们无法弄清楚为什么会出现此错误。如果有人可以提供帮助,那就太好了。谢谢!!!

更新 我尝试将 IList 更改为 List,但它没有做任何事情。这是我的课程和客户地址:

客户类:::

using System;

使用 System.Collections.Generic; 使用 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;
        }
    }
}

}

这是客户地址类::::

using System.IO;

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

命名空间 HannaPrintsDataAccess { 公共部分类 CustomerAddress { 公共字符串 ToXml() { XmlSerializer 序列化器 = new 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; }
}

}

再次感谢您的帮助

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

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

    你可能有:

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

    你改成的:

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

    不再有效。

    【讨论】:

    • 嘿,我试过了,但没用,我用两个类的代码更新了我的问题
    【解决方案2】:

    你可以放

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

    房产

    HannaPrintsDataAccess
    

    假设该成员实际上是List&lt;T&gt;的一个实例

    基本上,此属性是支持 Xml 序列化继承所必需的。也就是说,如果一个属性是 IList 类型,而实例是 List&lt;T&gt; 类型(继承自 IList),则需要将该属性添加到指定 List&lt;T&gt;. 的属性中

    【讨论】:

    • 您不能使用XmlInclude 属性来装饰属性。
    • 你能解释一下为什么不吗?
    • "这里需要注意的是,仅仅依靠继承是不够的。开发人员必须向 XmlSerializer 声明派生类型,方法是将 XmlInclude 属性附加到基类或将 XmlElement 属性附加到可以保存从声明类型派生的类型的对象。” @msdn.microsoft.com/en-us/library/aa302290.aspx
    • 嗨,我尝试了这个解决方案,但它没有用,我在我的问题中进行了编辑,提供了更多信息
    • 问题出在 Customer 类的 CustomerAddress 属性上。 Customer 上 CustomerAddress 的实际实例类型是什么?也就是说,如果您在断点处停止并键入 myCustomer.CustomerAddress.GetType().FullName,会打印什么?您需要将这种类型放入 CustomerAddress 属性的属性中。
    【解决方案3】:

    接口属性仍为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&lt;CustomerAddress&gt; 属性声明下方添加此声明:

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

    HTH...

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

    【讨论】:

    • 嘿,我试过了,它仍然给我同样的错误,我不明白为什么,因为我仔细检查了我的远程文件以确保更改通过,但它仍然有相同的错误消息,即使我将其更改为 List 而不是 IList。
    【解决方案4】:

    错误信息对我来说似乎很清楚:

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

    尝试将HannaPrintsDataAccess.Customer.CustomerAddresses 的类型(当前为IList&lt;CustomerAddress&gt;)替换为具体类型(例如List&lt;CustomerAddress&gt;)而不是接口。

    【讨论】:

    • 嗨,我尝试了这个解决方案,但它不起作用,我在我的问题中进行了编辑,提供了更多信息
    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 2016-12-11
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多