简体   繁体   English

Xml-RPC.Net响应映射与数组

[英]Xml-RPC.Net Response Mapping with array

I am trying to map Array response type from an XML-RPC, but I can't understand how to do it 我正在尝试从XML-RPC映射数组响应类型,但是我不知道该怎么做

The documentation about XML-RPC.NET library I am using is here http://xml-rpc.net/faq/xmlrpcnetfaq-2-5-0.html 我正在使用的有关XML-RPC.NET库的文档位于http://xml-rpc.net/faq/xmlrpcnetfaq-2-5-0.html

The example of response I get (int is always 0, and string contains random numbers) here: 我得到的响应示例(int始终为0,并且字符串包含随机数)在这里:

<methodResponse>
  <params>
    <param>
      <value>
        <array>
          <data>
            <value>
              <int>0</int>
            </value>
            <value>
              <string>9869117656.9552</string>
            </value>
          </data>
        </array>
      </value>
    </param>
  </params>
</methodResponse>

Here are my attempts in C# how to collect the response data 这是我在C#中如何收集响应数据的尝试

public struct try1 {
            public object[] returnstuff;
        }

public struct try2
        {
            public int returncode;
            public string token;
        }

But all these throw same exception: 但是所有这些都抛出相同的异常:

CookComputing.XmlRpc.XmlRpcTypeMismatchException'

Additional information: response contains array value where struct expected 附加信息:响应包含期望结构的数组值

Can you help me figure it out how to make correct C# structure to collect response inormation? 您能帮我弄清楚如何制作正确的C#结构来收集响应信息吗?

Try xml linq 试试xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Data data = new Data();
            data.values = doc.Descendants("data").Select(x => new Values() {
                returncode = (int?)x.Descendants("int").FirstOrDefault(),
                token = (string)x.Descendants("string").FirstOrDefault()
            }).ToArray();

        }
    }
    public class Data
    {
        public Values[] values;
    }

    public class Values
    {
        public int? returncode;
        public string token;
    }

}

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

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