I'm trying to rewrite a php function to C#. The php function converts an XML string to JSON.
At first, I came up with this:
string[] zips = { "27249","27215"}; // Request.Form.Get("zip").ToArray();
List<string> listings = new List<string>();
var webClient = new WebClient();
for (int i = 0; i != zips.Length; i++)
{
string returnXml = webClient.DownloadString("http://gateway.moviefone.com/movies/pox/closesttheaters.xml?zip=" + zips[i]);
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(returnXml);
listings.Add(json);
Response.Write(json);
}
But the output included encoded characters, which wasn't what I wanted.
Then, using JSON.NET, I replaced the loop body with this:
string returnXml = webClient.DownloadString("http://gateway.moviefone.com/movies/pox/closesttheaters.xml?zip=" + zips[i]);
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(returnXml);
var json = JsonConvert.SerializeXmlNode(xmlDoc);
listings.Add(json);
What bothers me about this is thatthe conversion of the Xml string to an XmlDocument just so that I can convert it back to a JSON string seems unnecessary. I want the JSON string for later use in a jQuery function.
How can I make the first version work just using intrinsic .NET methods?
Thanks.