[英]xml Deserialize a list of objects containing a list of objects in c#
我试图反序列化包含其他对象列表的对象列表。 我得到的是以下内容。 实际的反序列化由ApiContoller httpPost请求中的框架完成。 端点如下所示:
[HttpPost]
public async Task<HttpResponseMessage> MethodCall([FromBody] XmlEntries entries)
{
....
}
XmlEntries类如下所示:
[XmlRoot("Root")]
public class XmlEntries
{
[XmlArrayItem("XmlEntry")]
public List<XmlEntry> XmlEntries{ get; set; }
public XmlEntries()
{
XmlEntries = new List<XmlEntry>();
}
public XmlEntries(IEnumerable<XmlEntry> entries)
{
XmlEntries= entries.ToList();
}
}
XmlEntry类如下所示:
public class XmlEntry
{
[XmlArrayItem("XmlSubEntry")]
public List<XmlSubEntry> XmlSubEntries{ get; set; }
}
XmlSubEntry看起来像这样。
public class XmlSubEntry
{
string AttributeOne{ get; set; }
int? AttributeTwo{ get; set; }
}
我一直在使用提琴手发送以下XML
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<XmlEntries>
<XmlEntry>
<XmlSubEntries>
<XmlSubEntry>
<AttributeOne>P</AttributeOne>
<AttributeTwo>8</AttributeTwo>
</XmlSubEntry>
<XmlSubEntry>
<AttributeOne>S</AttributeOne>
<AttributeTwo>26</AttributeTwo>
</XmlSubEntry>
</XmlSubEntries>
</XmlEntry>
</XmlEntries>
</Root>
我的问题是XmlSubEntry的属性永远不会正确序列化。 当我调试apiController中的MethodCall条目时,将是一个包含1个XmlEntry的列表,而XmlSubEntries是2个XmlSubEntry的列表,但是属性(AttributeOne和AttributeTwo)始终为null。
我已经尝试过以所有方式对类进行注释,但是仍然无法获得用于序列化校正的属性。
周围有没有XML忍者可以帮助我弄清楚我在做什么错?
我能给你的最好的建议是相反的操作-添加一些数据并将其序列化为XML,看看它是什么样。 这通常会指出您错了。
但是,在这种情况下,您非常亲密。 唯一的问题是您无法序列化私有属性,因此请将它们公开:
public class XmlSubEntry
{
public string AttributeOne { get; set; }
public int? AttributeTwo { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.