我正在尝试将 json 字符串转换为 xml 1) 我的 json 是
[
{
"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) 我的 C# 代码是
XmlDocument doc = JsonConvert.DeserializeXmlNode(str);
出现以下错误:
Error:--XmlNodeConverter can only convert JSON that begins with an object.
我尝试在 json
中进行少量编辑,例如 remove []
问题元素。但没有奏效。
最佳答案
根据Mitchell Skurnik的 comment ,您可以使用 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
关于c# - 无法将 json 转换为 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35403588/