C# Xml 使用嵌套集合反序列化集合,嵌套集合为空

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

我正在消费第三方API。我正在尝试将 XML 字符串反序列化为 C# 复杂的 classes。这个 xml 字符串包含一个带有嵌套数组的数组。我成功序列化到外部列表(“描述”),但嵌套列表为空(“项目”)。我不确定是什么问题。

   <data boardid="abcdefg" listingid="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>
    </data>

C# Class 对象:

    [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("data")]
    public class Data
    {
        
        [XmlAttribute]
        public string boardid { get; set; }
        [XmlAttribute]
        public string listingid  { get; set; }
        [XmlAttribute]
        public string ExternalId { get; set; }
        [XmlArray("description")]
        [XmlArrayItem("group")]
        public List<Group> description { get; set; } = new List<Group>();

    }

编辑:根据接受的答案,解决方案是将组 class 更改为:

    [Serializable()]         
    [XmlRoot("group")]
    public class Group
    {
        [XmlAttribute]
        public string name { get; set; }
        
        [XmlElement("item")]
        public List<Item> item { get; set; }
    }

尝试以下操作:

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