WebApi XML反序列化-具有多个子节点的节点未正确反序列化为子节点对象的数组 [英] WebApi XML Deserialization - Node with multiple child nodes is not correctly deserialized as array of childnode objects
问题描述
我试图通过将XML POST-到WebApi POST方法来反序列化XML.一切正常,并且反序列化还可以,除了具有多个子节点的节点.
I'm trying to deserialize an XML by POST-ing it to an WebApi POST method. Everything works fine and deserializes okay, except for a node with multiple child nodes.
代码如下:
[Serializable]
public class grandparentnode
{
[XmlElement]
public parentnode[] parentnode { get; set; }
}
[Serializable]
public class parentnode
{
public childnode childnode { get; set; }
}
[Serializable]
public class childnode
{
public string foo { get; set; }
public string bar { get; set; }
public string baz { get; set; }
}
这是XML:
<grandparentnode>
<parentnode>
<childnode>
<foo>1</foo>
<bar>2</bar>
<baz>3</baz>
</childnode>
<childnode>
<foo>4</foo>
<bar>5</bar>
<baz>6</baz>
</childnode>
<childnode>
<foo>7</foo>
<bar>8</bar>
<baz>9</baz>
</childnode>
</parentnode>
</grandparentnode>
问题是,在调试时,我得到的只是 parentnode []
数组中的一项,如下所示: {WebApiListener.Controllers.FooBarBazController.parentnode [1]}
.我需要将parentnode作为childnode对象的数组.
The problem is, that on debug, all that I get is one item in the parentnode[]
array, like this: {WebApiListener.Controllers.FooBarBazController.parentnode[1]}
. I would need parentnode as an array of childnode objects.
知道为什么吗?
谢谢你,
Peter
推荐答案
正如@Jobo用户指出的那样,我混合了应反序列化数组的顺序.
As user @Jobo pointed out, I mixed up the order in which the array should be deserialized.
这是修改后的代码:
[Serializable]
public class grandparentnode
{
public parentnode parentnode { get; set; }
}
[Serializable]
public class parentnode
{
[XmlElement]
public childnode[] childnode { get; set; }
}
[Serializable]
public class childnode
{
public string foo { get; set; }
public string bar { get; set; }
public string baz { get; set; }
}
这篇关于WebApi XML反序列化-具有多个子节点的节点未正确反序列化为子节点对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!