简体   繁体   English

使用 RestSharp 反序列化 XML 序列

[英]Xml Sequence deserialization with RestSharp

I have this xml feed from an API with a XML sequence.我从带有 XML 序列的 API 获得了这个 xml 提要。

<?xml version="1.0" encoding="UTF-8" ?>
<Function>
    <Cmd>2002</Cmd>
    <Status>1</Status>
    <Cmd>2003</Cmd>
    <Status>0</Status>
    <Cmd>2004</Cmd>
    <Status>0</Status>
    <Cmd>1012</Cmd>
    <Status>3</Status>
    <Cmd>2006</Cmd>
    <Status>0</Status>
    <Cmd>2007</Cmd>
    <Status>0</Status>
    ...
</Function>

I already tried a few options for deserialization with Restsharp.我已经尝试了一些使用 Restsharp 进行反序列化的选项。 Ideally I would like to have something like the following, but it's obviously not working.理想情况下,我想要类似以下内容,但显然不起作用。

public class MyResponse
{
    public List<Setting> Settings { get; set;}
}

public class Setting
{
    public int Cmd { get; set; }
    public int Status { get; set; }
}

Thank you谢谢

You can use the DotNetXmlDeserializer of RestSharp to make Microsoft's XmlSerializer do the actual deserialization.你可以使用DotNetXmlDeserializer的 DotNetXmlDeserializer 来让微软的XmlSerializer做实际的反序列化。 Define your MyResponse class as follows, using XML serialization attributes to specify element names and also special handling for the Cmd/Status alternating sequence of elements:如下定义MyResponse类,使用XML 序列化属性来指定元素名称以及对Cmd/Status元素交替序列的特殊处理:

[XmlRoot("Function")]
public class MyResponse
{
    [XmlIgnore]
    public List<Setting> Settings { get; set; }

    /// <summary>
    /// Proxy property to convert Settings to an alternating sequence of Cmd / Status elements.
    /// </summary>
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [XmlAnyElement]
    public XElement[] Elements 
    {
        get
        {
            if (Settings == null)
                return null;
            return Settings.SelectMany(s => new[] { new XElement("Cmd", s.Cmd), new XElement("Status", s.Status) }).ToArray();
        }
        set
        {
            if (value == null)
                Settings = null;
            else
                Settings = value.Where(e => e.Name == "Cmd").Zip(value.Where(e => e.Name == "Status"), (cmd, status) => new Setting { Cmd = (int)cmd, Status = (int)status }).ToList();
        }
    }
}

Then deserialize as follows:然后反序列化如下:

        var serializer = new DotNetXmlDeserializer();
        var myResponse = serializer.Deserialize<MyResponse>(response);

Prototype fiddle .原型小提琴

Your XML doesn't match your object model.您的 XML 与您的对象模型不匹配。 There are two simple ways of fixing it * Make your XML response actually contain a list structure * Write a custom parser.有两种简单的修复方法 * 让您的 XML 响应实际上包含一个列表结构 * 编写自定义解析器。 ** https://github.com/restsharp/RestSharp/wiki/Deserialization ** https://github.com/restsharp/RestSharp/wiki/Deserialization

Who is responsible for generating the XML / Soap?谁负责生成 XML/Soap? Looks like something hand crafted.看起来像是手工制作的东西。

Look at the examples on the Restsharp page regarding the deserialisation of a list:查看 Restsharp 页面上有关列表反序列化的示例:

<?xml version="1.0" encoding="utf-8" ?>
<NestedListSample>
    <images>
        <image src="1.gif">value1</image>
        <image src="2.gif">value2</image>
        <image src="3.gif">value3</image>
        <image src="4.gif">value4</image>
    </images>
</NestedListSample>

Will map to the following C# schema:将映射到以下 C# 架构:

public class ListSample
{
    public List<Image> Images { get; set; }
}

public class Image
{
    public string Src { get; set; }
    public string Value { get; set; }
}

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

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