简体   繁体   中英

C# Xml Deserialize collection with nested collection, nested collection is null

I am consuming a third party API. I am trying to deserialize an XML string into C# complex classes. This xml string contains an array with a nested array. I am successful in serializing to the outer List ("Description"), but the nested List is null ("Item"). I am not sure what the issue is.

   <mlsdata mlsboardid="abcdefg" mlslistingid="23232323">
        <description>
          <group name="Address">
            <item displayname="Address" displayvalue="123 W Apple Ave" standardname="ad_street_address" />
            <item displayname="Address Direction" displayvalue="W" standardname="ad_street_direction" />
            ...            
          </group>
          <group name="Agent/Office">
            <item displayname="Agent First Name" displayvalue="John" standardname="ag_name_first" />
            <item displayname="Agent Last Name" displayvalue="Dor" standardname="ag_name_last" />
            ...
          </group>
          ...
        </description>
    </mlsdata>

C# Class Objects:

    [Serializable]
    [XmlRoot("item")]
    public class Item
    {
        [XmlAttribute("displayname")]
        public string displayname { get; set; }
        [XmlAttribute("displayvalue")]
        public string displayvalue { get; set; }
        [XmlAttribute("standardname")]
        public string standardname { get; set; }
    }
    
    [Serializable()]         
    [XmlRoot("group")]
    public class Group
    {
        [XmlAttribute]
        public string name { get; set; }
        
        [XmlArray("group")]
        [XmlArrayItem("item")]
        public List<Item> item { get; set; } // this is null
    }
    
    [Serializable]
    [XmlRoot("mlsdata")]
    public class MLSData
    {
        
        [XmlAttribute]
        public string mlsboardid { get; set; }
        [XmlAttribute]
        public string mlslistingid  { get; set; }
        [XmlAttribute]
        public string mlsExternalId { get; set; }
        [XmlArray("description")]
        [XmlArrayItem("group")]
        public List<Group> description { get; set; } = new List<Group>();

    }

Try following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication11
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            XmlSerializer serializer = new XmlSerializer(typeof(Bookstore));
            Bookstore store = (Bookstore)serializer.Deserialize(reader);
        }
  
    }
    [XmlRoot("bookstore")]
    public class Bookstore
    {
        [XmlElement("book")]
        public Book[] book { get; set; }
    }
             
    public class Book
    {
        [XmlAttribute]
        public string genre { get; set; }
        [XmlAttribute]
        public DateTime publicationdate { get; set; }
        [XmlAttribute]
        public string ISBN { get; set; }

        public decimal price { get; set; }

        public string title { get; set; }
        public Author author { get; set; }

    }
    
    public class Author
    {
        [XmlElement("first-name")]
        public string firstname { get; set; }
        [XmlElement("last-name")]
        public string lastname { 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