简体   繁体   English

xml反序列化包含c#中的对象列表的对象列表

[英]xml Deserialize a list of objects containing a list of objects in c#

I'm trying to deserialize a list of objects that in turn contain lists of other object. 我试图反序列化包含其他对象列表的对象列表。 What I've got is the following. 我得到的是以下内容。 The actual deserializing is done by the framework in a ApiContoller httpPost request. 实际的反序列化由ApiContoller httpPost请求中的框架完成。 The endpoint looks like this: 端点如下所示:

    [HttpPost]
    public async Task<HttpResponseMessage> MethodCall([FromBody] XmlEntries entries)
    { 
     .... 
    }

The XmlEntries class looks like this: 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();
    }
}

The XmlEntry class look like this: XmlEntry类如下所示:

public class XmlEntry
{
    [XmlArrayItem("XmlSubEntry")]
    public List<XmlSubEntry> XmlSubEntries{ get; set; }
}

and the XmlSubEntry looks like this. XmlSubEntry看起来像这样。

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

I've been using fiddler to send the following XML 我一直在使用提琴手发送以下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>

My problem is that the attributes of XmlSubEntry never gets correctly serialized. 我的问题是XmlSubEntry的属性永远不会正确序列化。 When i debug the MethodCall in apiController entries will be a list containing 1 XmlEntry with XmlSubEntries being a list of 2 XmlSubEntry, but the attributes (AttributeOne and AttributeTwo) are always null. 当我调试apiController中的MethodCall条目时,将是一个包含1个XmlEntry的列表,而XmlSubEntries是2个XmlSubEntry的列表,但是属性(AttributeOne和AttributeTwo)始终为null。

I have tried annotating the classes In all ways I can thing of but I still won't get the attributes to serialize correctry. 我已经尝试过以所有方式对类进行注释,但是仍然无法获得用于序列化校正的属性。

Is there any XML-ninjas around that can help me figure out what I'm doing wrong? 周围有没有XML忍者可以帮助我弄清楚我在做什么错?

The best tip I can give you is to do this in reverse - add some data and serialise it to XML, see what it looks like. 我能给你的最好的建议是相反的操作-添加一些数据并将其序列化为XML,看看它是什么样。 That will usually point to where you're wrong. 这通常会指出您错了。

In this case, however, you're very close. 但是,在这种情况下,您非常亲密。 The only issue you have is that you can't serialise private properties, so make them public: 唯一的问题是您无法序列化私有属性,因此请将它们公开:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM