finoutlook finoutlook - 1 year ago 105
JSON Question

Way to quickly check if string is XML or JSON in C#

I'm using C# in a console app and I need a quick way to check if a string being returned from another service is XML or JSON.

I know if it was just XML, I could check it against a schema, or if it was just JSON I could try to parse it with JSON.Net, but is there a quicker way - maybe using build in .Net functions - just to tell which it is before then going on to process it?

Answer Source

Very simple:

  1. Valid JSON starts always with '{' or '['
  2. Valid XML starts always with '<'

I'm talking about non-space data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download