2016-11-07 64 views
0

我试图反序列化对象又包含其他对象的名单列表的列表对象的列表。我得到的是以下内容。 实际的反序列化是由ApiContoller httpPost请求中的框架完成的。端点看起来是这样的:XML反序列化对象包含在C#

[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)总是零。

我试图诠释着班,我可以的事情,但我还是不会得到属性序列化correctry所有方式。

有没有解决任何XML的忍者可以帮我找出我做错了什么?

回答

0

最好的提示,我可以给你是做这个反向 - 添加一些数据并将其序列化到XML,看看是什么样子。这通常会指向你错在哪里。

在这种情况下,然而,你非常接近。你唯一的问题是,你不能序列化私人财产,所以让它们公开:

public class XmlSubEntry 
{ 
    public string AttributeOne { get; set; } 
    public int? AttributeTwo { get; set; } 
} 
+0

谢谢。我让它比我需要的更复杂。这个解决方案隐藏了我的视线。 :) –