I've got a legacy web service which I'd like to wrap with a new MVC Web API, question is can I get the ASP.NET Web API to convert my xml into json?
A thought that I had was to use XDocument to create a dynamic object and return that, but when I tried it with an ExpandoObject unfortunately it returned a json object with Key/Value pairs.
Using json.NET you can do it easily:
string result = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmldocument);
Download Newtonsoft.Json at http://james.newtonking.com/pages/json-net.aspx
You could. One way to do it would be to deserialize the XML into objects and then serialize them again into JSON.
A more efficient (though harder to code up approach) would be to write your own xml-to-json "transcriber" that reads in the XML and spits out JSON.
Just note that not all XML can be represented easily as JSON.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With