1

I recently used C# to build a web service client. I chose C# because the web services components are powerful and easy (much easier to work with than Gsoap or 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.

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
  2. 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.

4
  • Is your web service client serializing the data for the rest of your app to consume? Is the rest of your app in-process? If so, why serialize and deserialize it? Can you represent the data in an object model and just pass that/those objects back? Commented Apr 16, 2012 at 22:06
  • What kind of web service is this? ASMX or WCF? Commented Apr 16, 2012 at 22:12
  • @JMD the rest of my app is not in C# (mix of PHP and C), so C# objects will not work Commented Apr 17, 2012 at 2:20
  • @John Saunders Web service is SOAP and was written in Java, I used web service reference in C# to consume it, all I had to do was let it parse the WSDL Commented Apr 17, 2012 at 2:20

2 Answers 2

2

You surelly can use 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

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This works great. I tried this approach before but couldn't seem to get the Services.Web.Extensions to work. I gave it another shot and found this post: stackoverflow.com/questions/1987733/… Thanks a lot man!
1

Why don't you create DTO classes for the return value and use either JSON or XML serialization? It is very convenient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.