У меня есть следующий XML, который генерируется в SQL Server.Он широко использует атрибуты, поэтому я пытаюсь использовать XMLSerializer.Проблема, с которой я столкнулся, заключается в том, что она сериализует только один базовый массив под самым внешним элементом Entity и не может понять, почему.XML списка содержит множество массивов в элементе Entity, а сам элемент является рекурсивным, однако рекурсия, по-видимому, вообще не связана с этой проблемой.
Если я ввожу адреса электронной почты в базу данных, когдаон десериализован, имеет массив адресов электронной почты каждого адреса электронной почты, но не имеет массива сущностей.Если у него нет адресов электронной почты, возвращаемых из базы данных, то возвращается массив Entities.Есть и другие возвращаемые массивы, и каждый будет заполняться, если другие отсутствуют, но одновременно будет заполняться только один из массивов, и я не могу понять, почему.
Кроме того,все это работало с использованием DataContractSerializer, мы переключились только из-за необходимости в атрибутах.Он десериализует больше, чем один объект верхнего уровня, как и ожидалось, это все массивы под верхним уровнем, которые по какой-то причине не заполняются.
Заранее спасибо!
XML:
<XYZ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://schema.xyz.com/xyz" AuthorizedUserEntityID="1">
<Entities xmlns="https://schema.xyz.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity xmlns="https://schema.xyz.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Entity_Business" ID="158176" BusinessClassificationID="14" Name="ABC Business" EIN=" " EmployeeCount="0" TotalAssets="0.0000">
<Entity_Emails xmlns="https://schema.xyz.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity_Email ID="20148" EmailAddress="test1@test.com" />
<Entity_Email ID="20149" EmailAddress="test2@test.com" />
</Entity_Emails>
<Entities xmlns="https://schema.xyz.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity xmlns="https://schema.xyz.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Entity_Individual" ID="1" FName="Sam" MName="D" LName="Test" Entity_RelationshipTypeID="9">
<Entity_Emails xmlns="https://schema.xyz.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity_Email ID="20124" EmailAddress="stest@test.com" />
</Entity_Emails>
<Entities xmlns="https://schema.xyz.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</Entity>
</Entities>
</Entity>
</Entities>
</XYZ>
Это корневой класс:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace xyz.Classes
{
[DataContract]
[XmlRoot(ElementName = "XYZ", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class XYZ
{
[DataMember(Name = "Entities", IsRequired = true)]
[XmlArray("Entities")]
[XmlArrayItem("Entity")]
public List<Entity> Entities { get; set; }
[DataMember(Name = "AuthorizedUserEntityID", IsRequired = true)]
[XmlAttribute(AttributeName = "AuthorizedUserEntityID")]
public Int64 AuthorizedUserEntityID { get; set; }
[DataMember(Name = "ActiveEntityID", IsRequired = true)]
[XmlAttribute(AttributeName = "ActiveEntityID")]
public Int64 ActiveEntityID { get; set; }
[DataMember(Name = "ActiveEntityType", IsRequired = true)]
[XmlAttribute(AttributeName = "ActiveEntityType")]
public String ActiveEntityType { get; set; }
//[DataMember(Name = "Options", IsRequired = true)]
//[XmlElement("Options")]
//public Options Options { get; set; }
}
}
Это класс сущности:
using System.Collections.Generic;
using System.Xml.Serialization;
namespace xyz.Classes
{
[DataContract]
[XmlRoot("Entity", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
[XmlInclude(typeof(Entity_Business))]
[XmlInclude(typeof(Entity_Individual))]
public class Entity : Type
{
[DataMember(Name = "Entity_RelationshipTypeID", IsRequired = false)]
[XmlAttribute("Entity_RelationshipTypeID")]
public Int32 Entity_RelationshipTypeID { get; set; }
[DataMember(Name = "Entity_AuthorizedServices", IsRequired = false)]
[XmlArray("Entity_AuthorizedServices"), XmlArrayItem("Entity_AuthorizedService")]
public List<Entity_AuthorizedService> Entity_AuthorizedServices { get; set; }
[DataMember(Name = "Entity_Addresses", IsRequired = false)]
[XmlArray("Entity_Addresses")]
[XmlArrayItem("Entity_Address")]
public List<Entity_Address> Entity_Addresses { get; set; }
[DataMember(Name = "Entity_Emails", IsRequired = false)]
[XmlArray("Entity_Emails")]
[XmlArrayItem("Entity_Email")]
public List<Entity_Email> Entity_Emails { get; set; }
[DataMember(Name = "Entities", IsRequired = false)]
[XmlArray("Entities")]
[XmlArrayItem("Entity")]
public List<Entity> Entities { get; set; }
[DataMember(Name = "Entity_Phones", IsRequired = false)]
[XmlArray("Entity_Phones")]
[XmlArrayItem("Entity_Phone")]
public List<Entity_Phone> Entity_Phones { get; set; }
}
}
Это класс типа:
using System;
using System.Xml.Serialization;
namespace xyz.Classes
{
[XmlRoot("Type", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class Type
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("ID")]
public Int64 ID { get; set; }
}
}
Это класс Entity_Email:
using System;
using System.Xml.Serialization;
namespace ManageITWCF.Classes
{
[XmlRoot("Entity_Email", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class Entity_Email
{
[XmlAttribute("ID")]
public Int64 ID { get; set; }
[XmlAttribute("EmailAddress")]
public string EmailAddress { get; set; }
[XmlAttribute("Method")]
public String Method { get; set; }
}
}
Класс интерфейса:
using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;
using xyz.Classes;
namespace xyz
{
[ServiceContract(Namespace = "https://schema.xyz.com/xyz")]
public interface IsvcTest
{
[OperationContract]
[XmlSerializerFormat]
XMLReturn Login(string ApplicationName);
}
}
Класс XMLReturn:
using System;
using System.Xml.Serialization;
namespace xyz.Classes
{
[XmlRoot(ElementName = "XMLReturn")]
[Serializable()]
public class XMLReturn
{
[XmlAttribute("Success")]
public Boolean Success { get; set; }
[XmlAttribute("ErrorMessage")]
public string ErrorMessage { get; set; }
[XmlAttribute("ErrorPoint")]
public string ErrorPoint { get; set; }
[XmlElement("XYZ")]
public XYZ XYZ { get; set; }
}
}
Это класс Entity_Individual:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace xyz.Classes
{
[DataContract(Name = "Entity_Individual", Namespace = "https://schema.xyz.com/xyz")]
[XmlType("Entity_Individual", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class Entity_Individual : Entity
{
// [DataMember(Name = "isEnabled", IsRequired = true)]
[XmlAttribute("isEnabled")]
public bool isEnabled { get; set; }
//[DataMember(Name = "FName", IsRequired = true)]
[XmlAttribute("FName")]
public string FName { get; set; }
//[DataMember(Name = "MName", IsRequired = false)]
[XmlAttribute("MName")]
public string MName { get; set; }
//[DataMember(Name = "LName", IsRequired = true)]
[XmlAttribute("LName")]
public string LName { get; set; }
//[DataMember(Name = "SSN", IsRequired = false)]
[XmlAttribute("SSN")]
public string SSN { get; set; }
//[DataMember(Name = "DOB", IsRequired = false)]
[XmlAttribute("DOB")]
public DateTime DOB { get; set; }
}
}
Это класс Entity_Business:
using System;
using System.Collections.Generic;
//using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace xyz.Classes
{
// [DataContract(Name = "Entity_Business", Namespace = "https://schema.xyz.com/xyz")]
[XmlRoot("Entity_Business", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class Entity_Business : Entity
{
//[DataMember(Name = "EIN", IsRequired = true)]
[XmlAttribute("EIN")]
public string EIN { get; set; }
//[DataMember(Name = "BusinessClassificationID", IsRequired = true)]
[XmlAttribute("BusinessClassificationID")]
public Int16 BusinessClassificationID { get; set; }
// [DataMember(Name = "WebAddress", IsRequired = true)]
[XmlAttribute("WebAddress")]
public string WebAddress { get; set; }
//[DataMember(Name = "Equity", IsRequired = true)]
[XmlAttribute("Equity")]
public decimal Equity { get; set; }
}
}
Класс Entity_Address
using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace xyz.Classes
{
[DataContract]
[XmlRoot(ElementName = "Entity_Address", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class Entity_Address
{
[DataMember(Name = "ID", IsRequired = true)]
[XmlAttribute("ID")]
public Int64 ID { get; set; }
[DataMember(Name = "StreetAddress", IsRequired = true)]
[XmlAttribute("StreetAddress")]
public string StreetAddress { get; set; }
[DataMember(Name = "AptSuite", IsRequired = true)]
[XmlAttribute("AptSuite")]
public string AptSuite { get; set; }
[DataMember(Name = "City", IsRequired = true)]
[XmlAttribute("City")]
public string City { get; set; }
[DataMember(Name = "StateID", IsRequired = true)]
[XmlAttribute("StateID")]
public Int16 StateID { get; set; }
[DataMember(Name = "State", IsRequired = true)]
[XmlAttribute("State")]
public string State { get; set; }
[DataMember(Name = "ZipCode", IsRequired = true)]
[XmlAttribute("ZipCode")]
public string ZipCode { get; set; }
[DataMember(Name = "CountryID", IsRequired = true)]
[XmlAttribute("CountryID")]
public Int16 CountryID { get; set; }
[DataMember(Name = "Country", IsRequired = true)]
[XmlAttribute("Country")]
public string Country { get; set; }
[DataMember(Name = "Method", IsRequired = true)]
[XmlAttribute("Method")]
public string Method { get; set; }
[DataMember(Name = "AddressTypeID", IsRequired = true)]
[XmlAttribute("AddressTypeID")]
public Int16 AddressTypeID { get; set; }
[DataMember(Name = "AddressType", IsRequired = true)]
[XmlAttribute("AddressType")]
public string AddressType { get; set; }
}
}
Класс обслуживания Entity_Authorized
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace xyz.Classes
{
[XmlType("Entity_AuthorizedService", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class Entity_AuthorizedService : Type
{
//[DataMember(Name = "EntityID", IsRequired = true)]
[XmlAttribute("EntityID")]
public Int64 EntityID { get; set; }
//[DataMember(Name = "ServiceOrderTypeID", IsRequired = true)]
[XmlAttribute("ServiceOrderTypeID")]
public Int16 ServiceOrderTypeID { get; set; }
//[DataMember(Name = "Method", IsRequired = true)]
[XmlAttribute("Method")]
public string Method { get; set; }
}
}
Entity_Phoneкласс
using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace xyz.Classes
{
[DataContract]
[XmlType("Entity_Phone", Namespace = "https://schema.xyz.com/xyz")]
[Serializable()]
public class Entity_Phone
{
[DataMember(Name = "ID", IsRequired = true)]
[XmlAttribute("ID")]
public Int64 ID { get; set; }
[DataMember(Name = "PhoneTypeID", IsRequired = true)]
[XmlAttribute("PhoneTypeID")]
public Int16 PhoneTypeID { get; set; }
[DataMember(Name = "PhoneType", IsRequired = false)]
[XmlAttribute("PhoneType")]
public string PhoneType;
[DataMember(Name = "PhoneNumber", IsRequired = true)]
[XmlAttribute("PhoneNumber")]
public string PhoneNumber { get; set; }
[DataMember(Name = "Method", IsRequired = true)]
[XmlAttribute("Method")]
public String Method { get; set; }
}
}