简体   繁体   中英

Deserializing XML to Custom Nested Classes in .net Service

I'm having an issue deserializing XML from SQL Server. The XML is good; the code deserializing it is known working code; but for some reason it is only deserializing the first and second level, which has got to mean a mismatch of the class to the XML, but after staring at it I can't identify it.

This is an excerpt of the XML:

    <Options>
         <Countries>
             <Country ID="1" Name="Afghanistan" Code="AF" ISO="4" SubDivision="City" PostalName="Postal Code" />
             <Country ID="2" Name="Albania" Code="AL" ISO="8" SubDivision="City" PostalName="Postal Code" />
             <Country ID="3" Name="Algeria" Code="DZ" ISO="12" SubDivision="City" PostalName="Postal Code" />
             <Country ID="4" Name="American Samoa" Code="AS" ISO="16" SubDivision="City" PostalName="Postal Code" />
             <Country ID="5" Name="Andorra" Code="AD" ISO="20" SubDivision="City" PostalName="Postal Code" />
         </Countries>
         <EntityTypes>
             <EntityType ID="4" Name="Individual" />
             <EntityType ID="5" Name="Business" />
         </EntityTypes>
     </Options>

This is the Options class:

    namespace RecoverItWCF.Classes
    {
        [DataContract]
        [Serializable()]
        public class Options
        {
            [DataMember(Name = "Categories", IsRequired = false)]
            [XmlElement("Categories")]
            public Category[] Categories;
            [DataMember(Name = "Countries", IsRequired = false)]
            [XmlElement("Countries")]
            public Country[] Countries;
            [DataMember(Name = "EntityTypes", IsRequired = false)]
            [XmlElement("EntityTypes")]
            public EntityType[] EntityTypes;
        }
    }

This is the Country class:

namespace RecoverItWCF.Classes
{
    [DataContract]
    [Serializable()]
    public class Country
    {
        [DataMember(Name = "ID", IsRequired = true)]
        [XmlAttribute("ID")]
        public string ID { get; set; }

        [DataMember(Name = "Name", IsRequired = true)]
        [XmlAttribute("Name")]
        public string Name { get; set; }

        // truncated for brevity
     }
 }

This is the EntityType class:

namespace RecoverItWCF.Classes
    {
    [DataContract]
    [Serializable()]
    public class EntityType
        {
        [DataMember(Name = "ID")]
        [XmlAttribute("ID")]
        public Int16 ID { get; set; }

        [DataMember(Name = "Name")]
        [XmlAttribute("Name")]
        public string Name { get; set; }
    }
 }

The Options element does not contain a Categories element (correct), and does contain a Countries element (correct), but the Countries level has nothing below it (no Country array) when highlighted at breakpoint, and it should have 200 Country elements. It also contains an EntityTypes element, but nothing under it.

You should use the [XmlArrayItem] attribute :

[XmlArrayItem("Countries", IsNullable=true, Type = typeof(Country))]
public Country[] Countries;

Thank you to @Xiaoy312 for leading me down the right path. The correction needed to be made to the Options class. This got the job done.

    [DataMember(Name = "Countries", IsRequired = false)]
    [XmlArray("Countries")]
    [XmlArrayItem("Country")]
    public List<Country> Countries { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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