Reputation: 371
i am trying to convert json string to xml 1) my json is
[
{
"QuizTitle":"asdf",
"QuizImage":"",
"QuizCategory":"0",
"QuizTags":"asdf",
"question":[
[
{
"QuestionType":"1",
"QuestionTitle":"asdf",
"questionOption":[
{
"QuestionOptionValue":"sdf",
"QuestionOptionIsRight":"0"
},
{
"QuestionOptionValue":"asdf",
"QuestionOptionIsRight":"1"
}
]
}
],
[
{
"QuestionType":"2",
"QuestionTitle":"sdfdsf",
"questionOption":[
{
"QuestionOptionValue":"asdf",
"QuestionOptionIsRight":"0"
},
{
"QuestionOptionValue":"asdf",
"QuestionOptionIsRight":"1"
}
]
}
]
]
}
]
2) my c# code is
XmlDocument doc = JsonConvert.DeserializeXmlNode(str);
Getting following error:
Error:--XmlNodeConverter can only convert JSON that begins with an object.
i tried to little edit in json
like remove []
for question element. but not worked.
Upvotes: 4
Views: 8471
Reputation: 14789
According to the Mitchell Skurnik's comment, you can use JsonConvert.DeserializeXmlNode(JSONString, "root");
.
If your data is an array then you need to do something like this: JsonConvert.DeserializeXmlNode("{"Row":" + json + "}", "root").ToXmlString() otherwise you will get a "XmlNodeConverter can only convert JSON that begins with an object." exception.
– Mitchell Skurnik
Feb 17 '15 at 1:11
Upvotes: 16
Reputation: 330
This works when there is only one value in an array. This does not support multiple arrays on the parent node.
Upvotes: 0
Reputation: 1
<pre> public static string GetXMLFromJson(string jsonString)
{
string parentNote="Person";
XDocument doc = (XDocument)JsonConvert.DeserializeXNode("{\""+ parentNote + "\":" + json + "}", "DocumentElement");
return doc.ToString();
} </pre>
Upvotes: 0