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

问题描述 投票:0回答:5

我一直收到此错误:

异常详细信息: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(TypeDesctypeDesc,MemberInfo成员,类型型)+889917System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfopropertyInfo)+132System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo会员信息)+129System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping映射,StructModel模型,布尔openModel,字符串typeName,RecursionLimiter限制器)+1226System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel模型,String ns,布尔型openModel,XmlAttributes a,RecursionLimiter限制器)+460System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel模型,字符串ns,ImportContext上下文,字符串dataType,XmlAttributes a,布尔重复,布尔型openModel,RecursionLimiter限制器)+1415

[InvalidOperationException:错误反映类型'HannaPrintsDataAccess.Customer'。]System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel模型,字符串ns,ImportContext上下文,字符串dataType,XmlAttributes a,布尔重复,布尔型openModel,RecursionLimiter限幅器)+1621System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping访问器,FieldModel模型,XmlAttributes a,字符串ns,类型choiceIdentifierType,布尔rpc,布尔型openModel,RecursionLimiter限制器)+10296System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel父级,FieldModel模型,XmlAttributes a,字符串ns,RecursionLimiter限制器)+139System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping映射,StructModel模型,布尔openModel,字符串typeName,RecursionLimiter限制器)+1273

[InvalidOperationException:反映属性的错误'顾客'。]System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping映射,StructModel模型,布尔openModel,字符串typeName,RecursionLimiter限制器)+2167System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel模型,String ns,布尔型openModel,XmlAttributes a,RecursionLimiter限制器)+460System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel模型,字符串ns,ImportContext上下文,字符串dataType,XmlAttributes a,布尔重复,布尔型openModel,RecursionLimiter限制器)+1415

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

[InvalidOperationException:错误反映类型'HannaPrintsDataAccess.CustomerAddress []'。]System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel模型,字符串ns,ImportContext上下文,字符串dataType,XmlAttributes a,布尔重复,布尔型openModel,RecursionLimiter限幅器)+1621System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel模型,字符串ns,ImportContext上下文,字符串dataType,XmlAttributes a,RecursionLimiter限制器)+28System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel模型,XmlRootAttribute根,字符串defaultNamespace,RecursionLimiter限制器)+125System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type类型,XmlRootAttribute根,字符串defaultNamespace)+97System.Xml.Serialization.XmlSerializer..ctor(类型类型,字符串defaultNamespace)+461System.Xml.Serialization.XmlSerializer..ctor(类型类型)+6HannaPrintsBusiness.Services.Live.CustomerService.GetCustomerAddressesXml(CustomerAddress []customerAddresses)在E:\ ProjectsOld \ HannaPrints \ HannaPrintsBusiness \ Services \ Live \ CustomerService.cs:148CreateGreetingCard.orderButton_Click(Object发送者,EventArgs e)在c:\ HostingSpaces \ greetwus \ galadavetiye.com \ wwwroot \ HannaPrints \ HannaPrints \ WebUI \ CreateGreetingCard.aspx.cs:198System.Web.UI.WebControls.Button.OnClick(EventArgse)+111System.Web.UI.WebControls.Button.RaisePostBackEvent(StringeventArgument)+110System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(StringeventArgument)+10System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandlersourceControl,String eventArgument)+13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollectionpostData)+36System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔值includeStagesAfterAsyncPoint)+1565

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

UPDATE

我尝试将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序列化器=新的XmlSerializer(GetType());MemoryStream memoryStream =新的MemoryStream();serializer.Serialize(memoryStream,this);memoryStream.Seek(0,SeekOrigin.Begin);返回新的StreamReader(memoryStream).ReadToEnd();}

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

}

再次感谢您的帮助

我一直收到此错误:异常详细信息:System.NotSupportedException:无法序列化类型为System.Collections.Generic ....的成员HannaPrintsDataAccess.Customer.CustomerAddresses。

] <<<<<
由于错误消息明确告诉您XmlSerializer无法序列化接口。
您可能有:
public List<CustomerAddress> CustomerAddresses { get; set; }

您更改为:

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

不再有效。

您可以放入

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

HannaPrintsDataAccess

假设该成员实际上是List<T>的一个实例

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

的属性中

接口属性仍然是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 ...

P.S。为了将来参考,请记住,所有公共属性对xml序列化器都是可见的,并且它

will尝试对其进行序列化。确保将XmlIgnore属性放在不应序列化的属性上。

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

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

尝试用具体的类型(例如,HannaPrintsDataAccess.Customer.CustomerAddresses)代替接口来替换当前为IList<CustomerAddress>List<CustomerAddress>的类型。

我在$ .ajax请求中遇到了此类异常,contentType: "application/json; charset=utf-8"解决了我的问题。

c# asp.net
5个回答
1
投票
public List<CustomerAddress> CustomerAddresses { get; set; }

1
投票

1
投票

0
投票
异常详细信息:System.NotSupportedException:无法序列化类型为System.Collections.Generic.IList`1的成员HannaPrintsDataAccess.Customer.CustomerAddresses [[HannaPrintsDataAccess.CustomerAddress,HannaPrintsDataAccess,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null] ],因为它是一个接口。
© www.soinside.com 2019 - 2024. All rights reserved.