I'm requesting data from a REST API in C#. The API offers me the same data in a JSON and XML format.
For which one should I go? It's clear which one I would choose in JavaScript. But what in C#? Are there any performance benefits to help me choose one over the other in C# or any good-practice reasons?
Thanks in advance!
You should measure both to determine the performance characteristics of your situation.
In my own usage I started with JSON, for it's obvious 'crossplatform' advantages, but was forced to go with XML after getting major slowdowns on larger data sets in Silverlight/C# (this was reported as a bug in one of my modules where everything would freeze for several seconds - the slowdown was traced to the JSON deserialization of the large dataset it used). Switching to XML resulted in a larger download (gzip compression, which had been planned, was added at this point to counter this) but more then an order of magnitude increase in performance, even with the gzip decompression added.
Personally I would try both, measure the performance, compare and choose the one that's faster. From your application logic perspective this shouldn't really matter as it will be manipulating objects (not XML, nor JSON). The conversion of the XML/JSON to objects should be abstracted away into a repository which you could, within a blink of an eye, substitute with another implementation if you feel that the one you have chosen initially is slow or it doesn't work for you.
Personally I think that the XML serialization framework in C# is more mature and has a richer feature set than JSON support (for which there are actually two distinct .NET frameworks). But JSON is much more terse, so it depends on the characteristics of your load - whether it will be traffic-heavy, etc.
C# can deserialize objects from either JSON or XML.
As a general rule XML is going to be more verbose than JSON.
If you are getting back a small dataset, then it won't make much difference, but if you have a larger dataset, then the less verbose JSON might transfer more quickly across the wire.
I'd pull samples of each and compare.
At some point, possibly during debugging, you may want to inspect or display the raw API response. In that case, you are better off if you have chosen the format that you find easier to read.
Both JSON and XML have tons of libraries in any language, so the ease of parsing/generating is not very relevant here.
Also though JSON seems to be smaller, (specially if you don't use XML attributes but a pure-element approach) when gzipped both are almost the same
If you are really into speed, I suggest you check Google's protocol buffers. If not it's really a matter of taste, unless you plan to make AJAX calls to that API, in that case I'd pick JSON without doubt.