How to Serialize XML to a JSON object with Json.NET

Viewed 32023

I can serialize XML to a JSON string like this:

var xml = new XmlDocument();
xml.LoadXml("<person><name>John</name></person>");
string jsonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml, Newtonsoft.Json.Formatting.None);
Response.ContentType = "application/json";
Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(jsonString));

That would give me:

"{\"person\":{\"name\":\"John\"}}"

But how can I serialize it to a JSON object? Like this:

{"person":{"name":"John"}}
2 Answers
Related