【问题标题】:Deserialize one element of collection反序列化集合的一个元素
【发布时间】:2015-09-14 17:32:18
【问题描述】:

我正在反序列化来自 API 的 XML 响应。

<?xml version="1.0" encoding="UTF-8"?>
<categories type="array">
   <category>
      <parent-id />
      <name>Şirketiçi</name>
      <count type="integer">0</count>
      <elements-count type="integer">0</elements-count>
      <id type="integer">18940</id>
      <type>ProjectCategory</type>
   </category>
</categories>

我可以像上面那样使用下面的类来反序列化 XML 响应。

[XmlRoot("categories")]
public class CategoriesResponse :IEntityResponse
{
    public CategoriesResponse()
    {
        Categories = new List<Category>();
    }
    [XmlElement("category")]
    public List<Category> Categories { get; set; }
}

但我也收到了来自 API 的一个 Category 节点的响应,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<category>
   <parent-id />
   <name>Şirketiçi</name>
   <count type="integer">0</count>
   <id type="integer">18940</id>
   <elements_count type="integer">0</elements_count>
   <type>ProjectCategory</type>
</category>

所以我为此响应编写了一个具有 Category 属性的类,但无法反序列化。

[XmlRoot("category")]
public class CategoryResponse 
{
    public CategoryResponse()
    {

    }
    [XmlElement("category")]
    public Category Category;
    public string STATUS { get; set; }
}

我想使用 Category 类进行反序列化。我应该为这个 XML 响应使用哪个类定义。

【问题讨论】:

    标签: c# xml serialization


    【解决方案1】:

    第二个响应(类别)是根节点,不包含category 子节点。您应该将其反序列化为Category

    我猜你的 Category 类看起来像这样:

    [XmlRoot("category")]
    public class Category
    {
        public Category() { }
    
        [XmlElement("parent-id")]
        public int ParentId {get;set}
    
        [XmlElement("name")]
        public string Name {get;set}
    
        [XmlElement("count")]
        public int Count {get;set}
    
        [XmlElement("id")]
        public string Id {get;set}
    
        [XmlElement("elements_count")]
        public int ElementsCount {get;set}
    
        [XmlElement("type")]
        public string Type {get;set}
    }
    

    【讨论】:

    • 实际上我知道我可以反序列化 Category 类,但我正在使用基于 IEntityResponse 接口的通用方法来响应。所以我的反序列化类期望从 IEntityResponse 派生的参数而不是类。我不想更改我所有的反序列化方法或为不是从接口派生的类添加新方法。
    • 无论如何反序列化category 的类必须与我上面写的类相似。实施IEntityResponse,你应该已经准备好了。我在这里看不到其他真正的选择。
    【解决方案2】:

    您不需要序列化。在这种情况下,我会使用 XML Linq。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                        "<categories type=\"array\">" +
                           "<category>" +
                              "<parent-id />" +
                              "<name>Şirketiçi</name>" +
                              "<count type=\"integer\">0</count>" +
                              "<elements-count type=\"integer\">0</elements-count>" +
                              "<id type=\"integer\">18940</id>" +
                              "<type>ProjectCategory</type>" +
                           "</category>" +
                        "</categories>";
    
                XDocument doc = XDocument.Parse(input);
                List<Category> categories = new List<Category>();
    
                foreach (XElement category in doc.Descendants("category"))
                {
                    Category newCategory = new Category();
                    categories.Add(newCategory);
                    newCategory.parentID = category.Element("parent-id") == null  ? null : category.Element("parent-id").Value.Length == 0 ? null : (int?)category.Element("parent-id");
                    newCategory.name = category.Element("name") == null ? null : category.Element("name").Value.Length == 0 ? null : (string)category.Element("name");
                    newCategory.count = category.Element("count") == null ? null : category.Element("count").Value.Length == 0 ? null : (int?)category.Element("count");
                    newCategory.elementsCount = category.Element("elements-count") == null ? null : category.Element("elements-count").Value.Length == 0 ? null : (int?)category.Element("elements-count");
                    newCategory.id = category.Element("id") == null ? null : category.Element("id").Value.Length == 0 ? null : (int?)category.Element("id");
                    newCategory._type = category.Element("type") == null ? null : (string)category.Element("type");
                }
    
            }
            public class Category
            {
                public int? parentID { get; set; }
                public string name { get; set; }
                public int? count { get; set; }
                public int?  elementsCount { get; set; }
                public int? id { get; set; }
                public string _type { get; set; }
    
            }
        }
    }
    ​
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多