繁体   English   中英

XmlSerializer尝试在声明的类型之外进行序列化

[英]XmlSerializer trying to serialize outside of declared type

我有一个名为Location的类,我想由XmlSerializer进行序列化。 我在其他类上进行此工作,但是该特定类抛出了一个问题,即它无法序列化成员SmartApp.Contract.AccessorialCharges. AccessorialCharges类是使用Entity Framework v6.x在第三方提供的数据库上生成的。

所有这一切的疯狂部分- Location没有AccessorialCharges实体的直接属性或导航属性,并且从该AccessorialCharges实体到Location.没有任何联系Location.

在我看来,EF完全没有理由尝试序列化AccessorialCharges实体。

这是我用来进行序列化的语法:

var xml = new XmlSerializer(typeof(Location));  
const string filePath = @"C:\Users\me\Documents\";  
using (var writer = new StreamWriter($"{filePath}location.xml"))  
{  
    xml.Serialize(writer, location);  
}

XmlSerializer的声明中引发错误,错误消息的完整内容为: [NotSupportedException: Cannot serialize member SmartAppData.Contract.AccessorialCharges of type System.Collections.Generic.ICollection'1[[SmartAppData.AccessorialCharge, SmartAppData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.]

有没有人以前见过这种意外行为并能够提出解决方法? 我寻找了“强制” EF使用List <>而不是ICollection <>的方法,但这不是一个成功的搜索,并且不确定这是解决该问题的好方法,因为接口似乎更有效来到EF。

根据请求,这是位置类代码:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace SmartAppData.Entities._3XXXX
{
    [Serializable()]
    public class Location
    {

    public EntityHeader EntityHeader { get; set; }

    public string OrganizationName { get; set; }
    public bool ShouldSerializeOrganizationName()
    {
        return !string.IsNullOrEmpty(OrganizationName);
    }

    public string TradingPartnerNum { get; set; }
    public bool ShouldSerializeTradingPartnerNum()
    {
        return !string.IsNullOrEmpty(TradingPartnerNum);
    }

    public string TradingPartnerType { get; set; }
    public bool ShouldSerializeTradingPartnerType()
    {
        return !string.IsNullOrEmpty(TradingPartnerType);
    }

    public string LocNum { get; set; }
    public bool ShouldSerializeLocNum()
    {
        return !string.IsNullOrEmpty(LocNum);
    }

    public string LocationType { get; set; }
    public bool ShouldSerializeLocationType()
    {
        return !string.IsNullOrEmpty(LocationType);
    }

    public bool? IsActive { get; set; } = null;
    public bool ShouldSerializeIsActive()
    {
        return null != IsActive;
    }

    public bool? IsBillTo { get; set; } = null;
    public bool ShouldSerializeIsBillTo()
    {
        return null != IsBillTo;
    }

    public bool? IsRemitTo { get; set; } = null;
    public bool ShouldSerializeIsRemitTo()
    {
        return null != IsRemitTo;
    }

    public bool? IsCorporate { get; set; } = null;
    public bool ShouldSerializeIsCorporate()
    {
        return null != IsCorporate;
    }

    public string AddrName { get; set; }
    public bool ShouldSerializeAddrName()
    {
        return !string.IsNullOrEmpty(AddrName);
    }

    public string Addr1 { get; set; }
    public bool ShouldSerializeAddr1()
    {
        return !string.IsNullOrEmpty(Addr1);
    }

    public string Addr2 { get; set; }
    public bool ShouldSerializeAddr2()
    {
        return !string.IsNullOrEmpty(Addr2);
    }

    public string Addr3 { get; set; }
    public bool ShouldSerializeAddr3()
    {
        return !string.IsNullOrEmpty(Addr3);
    }

    public string CityName { get; set; }
    public bool ShouldSerializeCityName()
    {
        return !string.IsNullOrEmpty(CityName);
    }

    public string StateCode { get; set; }
    public bool ShouldSerializeStateCode()
    {
        return !string.IsNullOrEmpty(StateCode);
    }

    public string CountryIso2 { get; set; }
    public bool ShouldSerializeCountryIso2()
    {
        return !string.IsNullOrEmpty(CountryIso2);
    }

    public string PostalCode { get; set; }
    public bool ShouldSerializePostalCode()
    {
        return !string.IsNullOrEmpty(PostalCode);
    }

    public decimal? Latitude { get; set; } = null;
    public bool ShouldSerializeLatitude()
    {
        return null != Latitude;
    }

    public decimal? Longitude { get; set; } = null;
    public bool ShouldSerializeLongitude()
    {
        return null != Longitude;
    }

    /// <summary>
    /// Set from SmartDataEnums.TimeZoneType
    /// </summary>
    public string TimeZoneType { get; set; }
    public bool ShouldSerializeTimeZoneType()
    {
        return !string.IsNullOrEmpty(TimeZoneType);
    }

    public string CalendarName { get; set; }
    public bool ShouldSerializeCalendarName()
    {
        return !string.IsNullOrEmpty(CalendarName);
    }

    public string CalendarAppointmentName { get; set; }
    public bool ShouldSerializeCalendarAppointmentName()
    {
        return !string.IsNullOrEmpty(CalendarAppointmentName);
    }

    public int? CalendarOffsetDays { get; set; } = null;
    public bool ShouldSerializeCalendarOffsetDays()
    {
        return null != CalendarOffsetDays;
    }

    /// <summary>
    /// Set from SmartDataEnums.CommercialResidentialType
    /// </summary>
    public string CommercialResidentialType { get; set; }
    public bool ShouldSerializeCommercialResidentialType()
    {
        return !string.IsNullOrEmpty(CommercialResidentialType);
    }

    public bool? AllowsHazmat { get; set; } = null;
    public bool ShouldSerializeAllowsHazmat()
    {
        return null != AllowsHazmat;
    }

    public string HazmatContact { get; set; }
    public bool ShouldSerializeHazmatContact()
    {
        return !string.IsNullOrEmpty(HazmatContact);
    }

    public string HazmatPhone { get; set; }
    public bool ShouldSerializeHazmatPhone()
    {
        return !string.IsNullOrEmpty(HazmatPhone);
    }

    public int? NumPickupDocks { get; set; } = null;

    public int? NumDeliveryDocks { get; set; } = null;

    public bool? IsDeliveryAptRequired { get; set; } = null;
    public bool ShouldSerializeIsDeliveryAptRequired()
    {
        return null != IsDeliveryAptRequired;
    }

    public bool? IsPickupAptRequired { get; set; } = null;
    public bool ShouldSerializeIsPickupAptRequired()
    {
        return null != IsPickupAptRequired;
    }

    public SmartAppUomTypes.VolumeType VolumeCrossdockPoolMax { get; set; } = null;
    public bool ShouldSerializeVolumeCrossdockPoolMax()
    {
        return null != VolumeCrossdockPoolMax;
    }

    public SmartAppUomTypes.VolumeType VolumeCrossdockPoolMin { get; set; } = null;
    public bool ShouldSerializeVolumeCrossdockPoolMin()
    {
        return null != VolumeCrossdockPoolMin;
    }

    public SmartAppUomTypes.WeightType WeightCrossdockPoolMax { get; set; } = null;
    public bool ShouldSerializeWeightCrossdockPoolMax()
    {
        return null != WeightCrossdockPoolMax;
    }

    public SmartAppUomTypes.WeightType WeightCrossdockPoolMin { get; set; } = null;
    public bool ShouldSerializeWeightCrossdockPoolMin()
    {
        return null != WeightCrossdockPoolMin;
    }

    public string CrossdockPoolConstraint { get; set; }
    public bool ShouldSerializeCrossdockPoolConstraint()
    {
        return !string.IsNullOrEmpty(CrossdockPoolConstraint);
    }

    public bool? DoNotAllowMultistop { get; set; } = null;
    public bool ShouldSerializeDoNotAllowMultistop()
    {
        return null != DoNotAllowMultistop;
    }

    public string DefaultLtlDeliveryTime { get; set; }
    public bool ShouldSerializeDefaultLtlDeliveryTime()
    {
        return !string.IsNullOrEmpty(DefaultLtlDeliveryTime);
    }

    public string DefaultLtlPickupTime { get; set; }
    public bool ShouldSerializeDefaultLtlPickupTime()
    {
        return !string.IsNullOrEmpty(DefaultLtlPickupTime);
    }

    public string DefaultParcelGroundDelivery { get; set; }
    public bool ShouldSerializeDefaultParcelGroundDelivery()
    {
        return !string.IsNullOrEmpty(DefaultParcelGroundDelivery);
    }

    public string DefaultParcelPickup { get; set; }
    public bool ShouldSerializeDefaultParcelPickup()
    {
        return !string.IsNullOrEmpty(DefaultParcelPickup);
    }

    /// <summary>
    /// Set from SmartDataEnums.TradingPartnerType
    /// </summary>
    public string TradingPartnerDivision { get; set; }
    public bool ShouldSerializeTradingPartnerDivision()
    {
        return !string.IsNullOrEmpty(TradingPartnerDivision);
    }

    public string AltLocNum { get; set; }
    public bool ShouldSerializeAltLocNum()
    {
        return !string.IsNullOrEmpty(AltLocNum);
    }

    /// <summary>
    /// Set from SmartDataEnums.StopPreferenceType
    /// </summary>
    public string StopPreferencePickup { get; set; }
    public bool ShouldSerializeStopPreferencePickup()
    {
        return !string.IsNullOrEmpty(StopPreferencePickup);
    }

    /// <summary>
    /// Set from SmartDataEnums.StopPreferenceType
    /// </summary>
    public string StopPreferenceDelivery { get; set; }
    public bool ShouldSerializeStopPreferenceDelivery()
    {
        return !string.IsNullOrEmpty(StopPreferenceDelivery);
    }

    public string RoutingGuideGroup { get; set; }
    public bool ShouldSerializeRoutingGuideGroup()
    {
        return !string.IsNullOrEmpty(RoutingGuideGroup);
    }

    public bool? IsOnCreditHold { get; set; } = null;
    public bool ShouldSerializeIsOnCreditHold()
    {
        return null != IsOnCreditHold;
    }

    public bool? IgnoreCreditLimit { get; set; } = null;
    public bool ShouldSerializeIgnoreCreditLimit()
    {
        return null != IgnoreCreditLimit;
    }

    public SmartAppUomTypes.CurrencyType CreditLimit { get; set; } = null;
    public bool ShouldSerializeCreditLimit()
    {
        return null != CreditLimit;
    }

    public SmartAppUomTypes.CurrencyType OverAllowance { get; set; } = null;
    public bool ShouldSerializeOverAllowance()
    {
        return null != OverAllowance;
    }

    public SmartAppUomTypes.CurrencyType CreditUsed { get; set; } = null;
    public bool ShouldSerializeCreditUsed()
    {
        return null != CreditUsed;
    }

    public SmartAppUomTypes.CurrencyType CreditAvailable { get; set; } = null;
    public bool ShouldSerializeCreditAvailable()
    {
        return null != CreditAvailable;
    }

    public DateTime? LastCreditUpdateDate { get; set; } = null;
    public bool ShouldSerializeLastCreditUpdateDate()
    {
        return null != LastCreditUpdateDate;
    }

    public DateTime? LastPaymentDate { get; set; } = null;
    public bool ShouldSerializeLastPaymentDate()
    {
        return null != LastPaymentDate;
    }

    public string TerminalCode { get; set; }
    public bool ShouldSerializeTerminalCode()
    {
        return !string.IsNullOrEmpty(TerminalCode);
    }

    public string TerminalProCode { get; set; }
    public bool ShouldSerializeTerminalProCode()
    {
        return !string.IsNullOrEmpty(TerminalProCode);
    }

    [XmlArray("LocContacts")]
    [XmlArrayItem("LocContact")]
    public List<LocContact> LocContacts { get; set; }
    public bool ShouldSerializeLocContacts()
    {
        return null != LocContacts;
    }

    [XmlArray("LocComments")]
    [XmlArrayItem("LocComment")]
    public List<Comment> LocComments { get; set; }
    public bool ShouldSerializeLocComments()
    {
        return null != LocComments;
    }

    [XmlArray("LocRefNums")]
    [XmlArrayItem("LocRefNum")]
    public List<RefNum> LocRefNums { get; set; }
    public bool ShouldSerializeLocRefNums()
    {
        return null != LocRefNums;
    }

    [XmlArray("LocRelatedPartys")]
    [XmlArrayItem("LocRelatedParty")]
    public List<LocRelatedParty> LocRelatedPartys { get; set; }
    public bool ShouldSerializeLocRelatedPartys()
    {
        return null != LocRelatedPartys;
    }

    [XmlArray("LocActivitys")]
    [XmlArrayItem("LocActivity")]
    public List<LocActivity> LocActivitys { get; set; }
    public bool ShouldSerializeLocActivitys()
    {
        return null != LocActivitys;
    }
    }
}

using System;

namespace SmartAppData.Entities._3XXXX
{
    [Serializable()]
    public class EntityHeader
    {
        public DateTime? DateCreated { get; set; } = null;
        public bool ShouldSerializeDateCreated()
        {
            return null != DateCreated;
        }

        public string CreatedBy { get; set; }
        public bool ShouldSerializeCreatedBy()
        {
            return !string.IsNullOrEmpty(CreatedBy);
        }

        public DateTime? DateLastModified { get; set; }
        public bool ShouldSerializeDateLastModified()
        {
            return null != DateLastModified;
        }

        public string LastModifiedBy { get; set; }
        public bool ShouldSerializeLastModifiedBy()
        {
            return !string.IsNullOrEmpty(LastModifiedBy);
        }
    }
}

尝试这个

var xml = new XmlSerializer(typeof(Location));
const string filePath = @"C:\Users\me\Documents\";
using (TextWriter writeStream = new StreamWriter($"{filePath}location.xml");{  
    xml.Serialize(writeStream , location);
}

尝试删除[Serializable()]类注释,因为XmlSerializer不需要此注释

public class Location
{

}

然后

XmlSerializer serializer = new XmlSerializer(typeof(Location));
const string filePath = @"C:\Users\me\Documents\location.xml";
TextWriter writer = new StreamWriter(filePath);
serializer.Serialize(writer, location);
writer.Close();

暂无
暂无

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

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