Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Web API convert XML into Json?

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.

like image 815
nieve Avatar asked Jan 21 '26 14:01

nieve


2 Answers

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

like image 124
udidu Avatar answered Jan 23 '26 02:01

udidu


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.

like image 45
marcind Avatar answered Jan 23 '26 04:01

marcind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!