Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not covert json to xml

Tags:

json

c#

xml

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.

like image 208
user3248761 Avatar asked Dec 05 '22 01:12

user3248761


1 Answers

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

like image 196
Константин Ван Avatar answered Dec 25 '22 17:12

Константин Ван