简体   繁体   English

C#Web服务响应数组

[英]C# web service response arrays

I recently used C# to build a web service client. 我最近使用C#构建了一个Web服务客户端。 I chose C# because the web services components are powerful and easy (much easier to work with than Gsoap or Axis IMO). 我之所以选择C#,是因为Web服务组件功能强大且简单易用(比Gsoap或Axis IMO更容易使用)。

My C# app runs in the background and should return data that other pieces of my application (which includes a web app, a PLC, and a database) can store and use. 我的C#app在后台运行,应该返回我的应用程序的其他部分(包括Web应用程序,PLC和数据库)可以存储和使用的数据。

My responses look something like this: 我的回答看起来像这样:

[
 [
     string
     int
     []
 ]
 string
 []
 int
]

etc... 等等...

The point is that the return values are not simple. 关键是返回值并不简单。 My question is what is the best way to reformat and consume this data from other parts of my application? 我的问题是从我的应用程序的其他部分重新格式化和使用这些数据的最佳方法是什么?

My choices are: 我的选择是:

  1. XML XML
  2. JSON JSON

I've checked out both routes and have not found a great way to serialize this data. 我检查了两条路线,但没有找到一种很好的方法来序列化这些数据。 Can anyone provide any recommendations? 有人可以提供任何建议吗?

Worst case scenario I may need to just create a common routine to build XML with StringBuilder, but I'm hoping there's something I don't know about yet (I'm new to C#) that will make my life easy. 最糟糕的情况我可能需要创建一个使用StringBuilder构建XML的常用例程,但我希望还有一些我还不知道的东西(我是C#的新手)会让我的生活变得轻松。

You surelly can use JSON. 你当然可以使用JSON。

Use the JavaScriptSerializer class to serialize the data and your OK: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx 使用JavaScriptSerializer类来序列化数据和OK: http//msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

var data = new { name = "someValue", id = 1 };

var json = new System.Web.Script.Serialization.JavaScriptSerializer();

return json.Serialize(data);

I haven't tested this code (I'm not in my dev machine by now), but I'm sure it works with some adjustments to your needs. 我还没有测试过这段代码(我现在不在我的开发机器中),但我确信它可以根据您的需求进行一些调整。

Why don't you create DTO classes for the return value and use either JSON or XML serialization? 为什么不为返回值创建DTO类并使用JSON或XML序列化? It is very convenient. 这很方便。

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

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