18

I trying to convert JSON output into XML. Unfortunately I get this error:

JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.

This is what I up to now created.

string url = string.Format("https://graph.facebook.com/{0}?fields=posts.fields(message)&access_token={1}", user_name, access_token);

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    StreamReader reader = new StreamReader(response.GetResponseStream());
    jsonOutput = reader.ReadToEnd();
    Console.WriteLine("THIS IS JSON OUTPUT: " + jsonOutput);
}
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonOutput);
Console.WriteLine(doc);

And this is my JSON output:

{"id":"108013515952807","posts":{"data":[{"id":"108013515952807_470186843068804","created_time":"2013-05-14T20:43:28+0000"},{"message":"TEKST","id":"108013515952807_470178529736302","created_time":"2013-05-14T20:22:07+0000"}

How can I solve this problem?

Termininja
  • 6,620
  • 12
  • 48
  • 49
lukso
  • 589
  • 5
  • 13
  • 31

6 Answers6

38

Despite the fact your JSON provided in the question is not complete, you have multiple properties at the top level as indicated by the exception. You have to define the root for it to get valid XML:

var doc = JsonConvert.DeserializeXmlNode(jsonOutput, "root");

EDIT: In order to print out your XML with indentation you can use XDocument class from System.Xml.Linq namespace: XDocument.Parse(doc.InnerXml).

jwaliszko
  • 16,942
  • 22
  • 92
  • 158
  • Do you have an idea how to display XML? I get this System.Xml.XmlDocument :( – lukso May 15 '13 at 10:30
  • 1
    You can use `XDocument` class from `System.Xml.Linq` namespace to print out your XML with indentation: `XDocument.Parse(doc.InnerXml)`. – jwaliszko May 15 '13 at 11:19
4

I thought it's worth linking to the Documentation for turning xml to json and the other way around.

The guys are right..

// To convert an XML node contained in string xml into a JSON string   
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);

// To convert JSON text contained in string json into an XML node
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
3

You can do JSON-to-XML also by using the .NET Framework (System.Runtime.Serialization.Json):

private static XDocument JsonToXml(string jsonString)
{
    using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(jsonString)))
    {
        var quotas = new XmlDictionaryReaderQuotas();
        return XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(stream, quotas));
    }
}
Termininja
  • 6,620
  • 12
  • 48
  • 49
1

DeserializeXmlNode returns XDcument. If needed XNode use FirstNode.

//string jsonOutput="{"id":"108013515952807","posts":{"data":[{"id":"108013515952807_470186843068804","created_time":"2013-05-14T20:43:28+0000"},{"message":"TEKST","id":"108013515952807_470178529736302","created_time":"2013-05-14T20:22:07+0000"}";
var myelement= JsonConvert.DeserializeXmlNode(jsonOutput, "myelement").FirstNode;
tetris
  • 121
  • 1
  • 3
0

Your shared JSON is invalid please go through http://jsonformatter.curiousconcept.com/ and validate your JSON first.

Yourt JSON should look like:

{
   "id":"108013515952807",
   "posts":{
      "data":[
         {
            "id":"108013515952807_470186843068804",
            "created_time":"2013-05-14T20:43:28+0000"
         },
         {
            "message":"TEKST",
            "id":"108013515952807_470178529736302",
            "created_time":"2013-05-14T20:22:07+0000"
         }
      ]
   }
}
Navin Rawat
  • 3,208
  • 1
  • 19
  • 31
0

Adding on @jwaliszko's answer, converting json to XDocument:

XDocument xml = JsonConvert.DeserializeXNode(json);
OfirD
  • 9,442
  • 5
  • 47
  • 90