c# - 无法将 json 转换为 xml

标签 c# json xml

我正在尝试将 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 Skurnikcomment ,您可以使用 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/

相关文章:

c# - HttpWebRequest 的并发限制

javascript - 防止 jquery 和/或 x-editable 将字符串解析为对象

xml - JAXB:两个版本的 XSD,一种对象模型

c# - NHibernate - 无法执行查询

c# - 在 ASP.NET 4.5 中将 LINQ 与 MySql 结合使用

C# + N单元 : Unit testing methods with byte array arguments

python迭代json结构和键值未知的json文件

java - 让 gson 不要在字段名称周围加上引号

javascript - jQuery XML 解析/操作 IE8 错误

xml - 任何想法为什么图案化的 SVG 文件在浏览器中显示为空白?