Deserialize Json String to xml document

Nov 12, 2010 at 11:12 AM

Hi Guys

I have taken up a project to integration a .Net web app to a 3rd party system asset Management solution with an API that returns Json when I call a service.

I am having trouble getting the DeserialiseXmlNode method to work - I am not getting any XML returned when a pass in the Json string to the function, the xml document is empty

to add, I have tried passing through different Json strings, some much larger, and I do not get anything returned AT ALL, no a clue why.

any ideas guys?

Here's my code:

using Newtonsoft.Json;
using System.Xml;

namespace AssetManagementC
{
    public class JsonConvertor
    {
        public XmlDocument Deserialize (string Json)
        {
            XmlDocument xmldoc = (XmlDocument)JsonConvert.DeserializeXmlNode(Json);

            return xmldoc;

        }

    }
}

Here is the Json string I pass in, taken from here http://james.newtonking.com/projects/json/help/ - DeserializeXMLNode

"{
  ""?xml"": {
    ""@version"": ""1.0"",
    ""@standalone"": ""no""
  },
  ""root"": {
    ""person"": [
      {
        ""@id"": ""1"",
        ""name"": ""Alan"",
        ""url"": ""http://www.google.com""
      },
      {
        ""@id"": ""2"",
        ""name"": ""Louis"",
        ""url"": ""http://www.yahoo.com""
      }
    ]
  }
}"

 

Nov 12, 2010 at 1:47 PM

something else I have just spotted while running a test, exception:

Newtonsoft.Json.JsonSerializationException: XmlNodeConverter can only convert JSON that begins with an object.. 

Any ideas guys?