[英]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.