C# XmlSerializer尝试在声明的类型之外序列化
我有一个名为C# XmlSerializer尝试在声明的类型之外序列化,c#,entity-framework,serialization,xmlserializer,C#,Entity Framework,Serialization,Xmlserializer,我有一个名为Location的类,我希望通过XmlSerializer对其进行序列化。我在其他类上也有这样的工作,但是这个特定类引发了一个问题,它无法序列化memberSmartApp.Contract.AccessorialCharges。这个AccessorialCharges类是在第三方提供的数据库上使用Entity Framework v6.x生成的 关于这一切的疯狂部分-位置没有直接属性,也没有指向辅助收费实体的导航属性,也没有从辅助收费实体到位置的连接。 在我看来,EF根本没有理由尝
Location
的类,我希望通过XmlSerializer对其进行序列化。我在其他类上也有这样的工作,但是这个特定类引发了一个问题,它无法序列化memberSmartApp.Contract.AccessorialCharges。
这个AccessorialCharges
类是在第三方提供的数据库上使用Entity Framework v6.x生成的
关于这一切的疯狂部分-位置
没有直接属性,也没有指向辅助收费
实体的导航属性,也没有从辅助收费
实体到位置的连接。
在我看来,EF根本没有理由尝试序列化附件费用
实体
下面是我用来进行序列化的语法:
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:无法序列化System.Collections.Generic.ICollection'1类型的成员SmartAppData.Contract.AccessorAlCharges'1[[SmartAppData.AccessorialCharge,SmartAppData,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]因为它是一个接口。]
以前有没有人见过这种意外的行为,并能想出一个解决办法?我想办法“强制”EF使用列表而不是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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Xml.Serialization;
命名空间SmartAppData.Entities.\u 3xxx
{
[可序列化()]
公共类位置
{
公共EntityHeader EntityHeader{get;set;}
公共字符串OrganizationName{get;set;}
公共布尔值应序列化OrganizationName()
{
return!string.IsNullOrEmpty(OrganizationName);
}
公共字符串TradingPartnerNum{get;set;}
公共bool应该序列化TradingPartnerNum()
{
return!string.IsNullOrEmpty(TradingPartnerNum);
}
公共字符串TradingPartnerType{get;set;}
公共bool应序列化TradingPartnerType()
{
return!string.IsNullOrEmpty(TradingPartnerType);
}
公共字符串LocNum{get;set;}
公共bool应序列化locnum()
{
return!string.IsNullOrEmpty(LocNum);
}
公共字符串位置类型{get;set;}
公共布尔值应序列化LocationType()
{
return!string.IsNullOrEmpty(LocationType);
}
public bool?IsActive{get;set;}=null;
公共布尔值应序列化为活动()
{
返回null!=IsActive;
}
公共bool?IsBillTo{get;set;}=null;
公共布尔值应序列化为BillTo()
{
返回null!=IsBillTo;
}
公共bool?IsRemitTo{get;set;}=null;
公共布尔值应序列化为()
{
返回null!=IsRemitTo;
}
公共布尔?IsCorporate{get;set;}=null;
公共学校应为公司()
{
返回null!=IsCorporate;
}
公共字符串AddrName{get;set;}
公共bool应该序列化addrname()
{
return!string.IsNullOrEmpty(AddrName);
}
公共字符串Addr1{get;set;}
公共bool应序列化addr1()
{
return!string.IsNullOrEmpty(Addr1);
}
公共字符串Addr2{get;set;}
公共bool应序列化addr2()
{
return!string.IsNullOrEmpty(Addr2);
}
公共字符串Addr3{get;set;}
公共bool应序列化addr3()
{
return!string.IsNullOrEmpty(Addr3);
}
公共字符串CityName{get;set;}
公共bool应该序列化cityname()
{
return!string.IsNullOrEmpty(CityName);
}
公共字符串状态码{get;set;}
public bool ShouldSerializeStateCode()
{
return!string.IsNullOrEmpty(StateCode);
}
公共字符串CountryIso2{get;set;}
公共布尔值应为ISO2()
{
return!string.IsNullOrEmpty(CountryIso2);
}
公共字符串PostalCode{get;set;}
公共bool应序列化PostalCode()
{
return!string.IsNullOrEmpty(PostalCode);
}
公共十进制?纬度{get;set;}=null;
公共布尔值应为()
{
返回null!=纬度;
}
公共十进制?经度{get;set;}=null;
公共布尔值应为()
{
返回null!=经度;
}
///
///从SmartDataEnums.TimeZoneType设置
///
公共字符串时区类型{get;set;}
公共布尔值应序列化TimeZoneType()
{
return!string.IsNullOrEmpty(TimeZoneType);
}
公共字符串CalendarName{get;set;}
公共布尔值应序列化日历名称()
{
return!string.IsNullOrEmpty(CalendarName);
}
公共字符串CalendarAppointName{get;set;}
公共bool应序列化CalendarAppointName()
{
return!string.IsNullOrEmpty(CalendarAppointmentName);
}
public int?CalendarOffsetDays{get;set;}=null;
公共布尔值应为日历OffsetDays()
{
返回null!=CalendarOffsetDays;
}
///
///从SmartDataEnums.CommercialResidentialType设置
///
公共字符串CommercialResidentialType{get;set;}
public bool应序列化CommercialResidentialType()
{
return!string.IsNullOrEmpty(CommercialResidentialType);
}
public bool?allowshadmat{get;set;}=null;
公共场所应设置危险品()
{
返回null!=允许危险品;
}
公共字符串{get;set;}
公共图书馆应提供联系方式()
{
return!string.IsNullOrEmpty(HazmatContact);
}
公共街
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);
}
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();