代码之家  ›  专栏  ›  技术社区  ›  anthonypliu

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

  •  0
  • anthonypliu  · 技术社区  · 15 年前

    我经常会遇到这样的错误:

    System.NotSupportedException异常:无法 序列化成员 小时annaPrintsDataAccess.Customer.CustomerAddresses 类型 HannaPrintsDataAccess公司, 版本=1.0.0.0,区域性=中性, 接口。

    以下是堆栈跟踪: [NotSupportedException:无法 小时annaPrintsDataAccess.Customer.CustomerAddresses 版本=1.0.0.0,区域性=中性, PublicKeyToken=null]],因为它是

    序列化成员 “H”annaPrintsDataAccess.Customer.CustomerAddresses' '系统.集合.通用.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess公司,
    System.Xml.Serialization文件.结构模型.CheckSupportedMember(类型描述) typeDesc,MemberInfo成员,类型 型号)+889917
    物业信息)+132
    System.Xml.Serialization文件.StructModel.GetFieldModel(会员信息)
    System.Xml.Serialization文件XmlReflectionImporter.InitializeStructMembers(结构映射) 映射,结构模型,布尔 openModel,字符串typeName,
    System.Xml.Serialization文件XmlReflectionImporter.ImportStructLikeMapping映射(结构模型) model,String ns,Boolean openModel, 限制器)+460
    System.Xml.Serialization文件XmlReflectionImporter.ImportTypeMapping文件(型号) 上下文,字符串数据类型, XMLA属性,布尔重复, 布尔openModel,递归限制器 限制器)+1415

    错误反射类型 “H”annaPrintsDataAccess.客户'.]
    System.Xml.Serialization文件XmlReflectionImporter.ImportTypeMapping文件(型号) 上下文,字符串数据类型, 布尔openModel,递归限制器 限制器)+1621
    System.Xml.Serialization文件XmlReflectionImporter.ImportAccessorMapping映射(成员映射) choiceIdentifierType,布尔rpc, 布尔openModel,递归限制器 限制器)+10296
    System.Xml.Serialization文件XmlReflectionImporter.ImportFieldMapping文件(结构模型) 递归限制器)+139
    映射,结构模型,布尔 openModel,字符串typeName, 递归限制器)+1273

    '客户'。]
    System.Xml.Serialization文件XmlReflectionImporter.InitializeStructMembers(结构映射) openModel,字符串typeName,
    System.Xml.Serialization文件XmlReflectionImporter.ImportStructLikeMapping映射(结构模型) model,String ns,Boolean openModel, XMLA属性,递归限制器 限制器)+460
    上下文,字符串数据类型, XMLA属性,布尔重复, 布尔openModel,递归限制器 限制器)+1415

    [InvalidOperationException:有 错误反射类型 “H”annaPrintsDataAccess.CustomerAddress'.] System.Xml.Serialization文件XmlReflectionImporter.ImportTypeMapping文件(型号) 模型,字符串ns,导入上下文 上下文,字符串数据类型, XMLA属性,布尔重复, 限制器)+1621
    数组映射,XmlArrayItemAttributes 字符串数组元素,
    System.Xml.Serialization文件XmlReflectionImporter.ImportarrayLike映射(阵列模型) 模型,字符串ns,递归限制器 限制器)+207
    System.Xml.Serialization文件XmlReflectionImporter.ImportTypeMapping文件(型号) 模型,字符串ns,导入上下文 布尔openModel,递归限制器 限制器)+1143

    错误反射类型 “H”annaPrintsDataAccess.CustomerAddress[]'.] 模型,字符串ns,导入上下文 上下文,字符串数据类型, 布尔openModel,递归限制器
    System.Xml.Serialization文件XmlReflectionImporter.ImportTypeMapping文件(型号) 模型,字符串ns,导入上下文 XMLA属性,递归限制器 限制器)+28
    模型,XmlRootAttribute根,字符串 defaultNamespace,递归限制器
    System.Xml.Serialization文件XmlReflectionImporter.ImportTypeMapping文件(类型
    System.Xml.Serialization文件.XmlSerializer..ctor(类型 类型,字符串defaultNamespace)+461
    类型)+6
    客户地址) 电子邮箱:\projectsell\HannaPrints\HannaPrintsBusiness\Services\Live\客户服务.cs:148 CreateGreetingCard.orderButton\单击(对象) 发件人,事件参数(e)在 c:\HostingSpaces\greetwus\galadavetiye.com网站\wwwroot\HannaPrints\HannaPrints\WebUI\CreateGreetingCard.aspx.cs:198 系统.Web.UI.WebControls.Button.OnClick(事件参数)
    系统.Web.UI.WebControls.Button.RaisePostBackEvent事件(字符串) 事件参数)+110
    系统.Web.UI.网络控制按钮系统.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数)+10
    sourceControl,字符串事件参数) +13 系统.Web.UI.Page.RaisePostBackEvent事件(名称值集合) 邮政数据)+36
    系统.Web.UI.页面.ProcessRequestMain(布尔值) includeStagesBeforeAsyncPoint,布尔值 包括StagesAfterAsyncPoint)+1565

    更新 我尝试将IList更改为List,但它没有任何作用。以下是我的客户地址:

    客户类别:::

    using System;
    

    使用Castle.ActiveRecord文件;

    { 公共部分类客户

        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;
            }
        }
    }
    

    }

    这是Customer Address类::

    using System.IO;
    

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

    命名空间访问 { 公共部分类CustomerAddress { 公共字符串ToXml() XmlSerializer serializer=新的XmlSerializer(GetType()); MemoryStream MemoryStream=新的MemoryStream(); 序列化程序。序列化(memoryStream,这个); 内存流。查找(0, SeekOrigin。开始); }

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

    4 回复  |  直到 15 年前
        1
  •  1
  •   Darin Dimitrov    15 年前

    XmlSerializer 无法序列化接口。

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

    您更改为:

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

    这已经不起作用了。

        2
  •  1
  •   Jeff    15 年前

    你可以把

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

    关于财产

    HannaPrintsDataAccess
    

    List<T>

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

        3
  •  1
  •   code4life    15 年前

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

    嗯。。。

    另请记住,所有公共属性对xml序列化程序都是可见的,并且 尝试序列化它们。一定要把

        4
  •  0
  •   Heinzi    15 年前

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

    尝试替换 HannaPrintsDataAccess.Customer.CustomerAddresses ,当前为 IList<CustomerAddress> ,混凝土类型(例如。 List<CustomerAddress> )而不是界面。

        5
  •  0
  •   IHAFURR    6 年前

    contentType: "application/json; charset=utf-8" 解决了我的问题。