0

I create simple c# code to convert json to xml but there is an issue. Check the result that miss xml header(version and encoding)

string json = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";
XmlDocument doc = (XmlDocument) Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json, "root", true);
string xmlString = doc.InnerXml;
Console.log(xmlString);

Result

<root><name>JSON</name><integer>1</integer><double>2</double><boolean>true</boolean><nested><id>42</id></nested><array>1</array><array>2</array><array>3</array></root>
dbc
  • 104,963
  • 20
  • 228
  • 340
KBH
  • 1,887
  • 2
  • 11
  • 11
  • 1
    By default the header is not created when you generate xml from json. You can add header declaration manually. [This](https://stackoverflow.com/questions/14839630/how-to-get-xml-with-header-xml-version-1-0) might help you. – Chetan Jan 07 '19 at 23:52
  • 1
    I think the [second answer](https://stackoverflow.com/a/14840090/3744182) to [How to get XML with header ( – dbc Jan 08 '19 at 06:44
  • 1
    Demo of using [the second answer](https://stackoverflow.com/a/14840090/3744182) here: https://dotnetfiddle.net/Rocu0f – dbc Jan 08 '19 at 09:05

0 Answers0