我有一个名为Location
的类,我希望由XmlSerializer序列化。我有这个在其他类上工作,但是这个特定的类抛出了一个问题,它无法序列化成员SmartApp.Contract.AccessorialCharges.
AccessorialCharges
类是使用Entity Framework v6.x在由数据库生成的第三方。
关于所有这一切的疯狂部分 - Location
没有AccessorialCharges
实体的直接属性和导航属性,并且AccessorialCharges
实体与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);
}
}
}
答案 0 :(得分:0)
试试这个
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);
}
答案 1 :(得分:0)
尝试删除[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();