Tuesday, March 3, 2015

JSON to XML conversion : Error "This document already has a ' DocumentElement ' node"

 Today I had faced an  error while converting JSON string into an XML document object using JSON.net Jsonconvert.

Here is the code snippet

        FacebookClient fbclient = new FacebookClient();
        JObject objFeeb = JObject.Parse(@" " + fbclient.Get("me/feed") + " ");
        XmlDocument doc = JsonConvert.DeserializeXmlNode(objFeeb.ToString());

When I run the above code I was getting an error as  "This document already has a ' DocumentElement ' node"

To fix the issue, I  just have defined the root element of XML doc and it worked !!

Here is the working code.

  XmlDocument doc = JsonConvert.DeserializeXmlNode(objFeeb.ToString(), "FacebookFeed");