WebApi XML反序列化-具有多个子节点的节点未正确反序列化为子节点对象的数组 [英] WebApi XML Deserialization - Node with multiple child nodes is not correctly deserialized as array of childnode objects

查看:36
本文介绍了WebApi XML反序列化-具有多个子节点的节点未正确反序列化为子节点对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆