繁体   English   中英

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

[英]C# Xml Deserialize collection with nested collection, nested collection is null

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

   <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 对象:

    [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>();

    }

尝试以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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