WCF XMLSerializer仅反序列化一个基础数组

时间:2018-12-04 17:32:00

标签: c# xml wcf deserialization xmlserializer

我有以下在SQL Server中生成的XML。它广泛使用属性,因此我正在尝试使用XMLSerializer。我遇到的问题是,它仅序列化了最外层Entity元素下的单个基础数组,却无法弄清原因。列表XML在Entity元素下包含许多数组,并且该元素本身是递归的,但是,递归似乎根本与问题无关。

如果我在数据库中输入电子邮件地址,则将其反序列化时,它具有每个电子邮件地址的电子邮件地址数组,但没有Entities数组。如果它没有从数据库返回任何电子邮件地址,则返回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; }       

    }
}

这是Entity类:

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

这是Type类:

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

0 个答案:

没有答案
相关问题