【问题标题】:Xml-RPC.Net Response Mapping with arrayXml-RPC.Net 响应映射与数组
【发布时间】:2016-10-20 20:24:27
【问题描述】:

我正在尝试从 XML-RPC 映射数组响应类型,但我不明白该怎么做

关于我正在使用的 XML-RPC.NET 库的文档在这里 http://xml-rpc.net/faq/xmlrpcnetfaq-2-5-0.html

我在这里得到的响应示例(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>

这是我在 C# 中如何收集响应数据的尝试

public struct try1 {
            public object[] returnstuff;
        }

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

但是所有这些都抛出相同的异常:

CookComputing.XmlRpc.XmlRpcTypeMismatchException'

附加信息:响应包含预期结构的数组值

你能帮我弄清楚如何制作正确的 C# 结构来收集响应信息吗?

【问题讨论】:

    标签: c# xml xml-rpc


    【解决方案1】:

    试试 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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2011-07-21
      • 1970-01-01
      相关资源
      最近更新 更多