【发布时间】: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