反序列化集合的一个元素

时间:2015-09-14 17:32:19

标签: c# xml serialization

我正在从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获得了下面一个类别节点的响应。

<?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响应。

2 个答案:

答案 0 :(得分:0)

第二个响应(类别)是根节点,不包含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}
}

答案 1 :(得分:0)

您不需要序列化。在这种情况下我会使用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; }

        }
    }
}
​