Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert json string to xml in c# and parse the data from acctNo and availBal node in the same.

But i am getting exception
JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName.
My json string is as below:
<pre>{
  "results": [
    {
      "rimNo": "1220551",
      "rimName": "YOGESH  SHAH",
      "rimType": "NonPersonal",
      "rimStatus": "Active",
      "rimClass": "Elite-High Networth Individual",
      "acctNo": "AE590230000001005506660",
      "acctTitle": "YOGESH   SHAH",
      "acctType": "CIA",
      "acctStatus": "Active",
      "acctClass": "CK-Elite-NIB-Res-AED",
      "acctBrNo": "99",
      "acctBranch": "Head Office",
      "segment": "CBD Elite",
      "availBal": "878.2900",
      "currency": "AED",
      "nationality": "India",
      "phone1": "",
      "phone2": "",
      "mobile": "971507587623",
      "email1": "YOGESH@EMAIL.COM",
      "email2": "",
      "ref": "REF20240501154316770",
      "reserved1": "",
      "reserved2": "",
      "reserved3": ""
    }
  ],
  "errors": [
    {
      "errorCode": "000",
      "errorMessage": "Success"
    }
  ]
}


What I have tried:

My code isas below:
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
Posted

1 solution

Read the error message, it's very clear:
Error
JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName.
Your JSON data contains two root elements: "Error" and "Result" - XML doesn't allow that, you need to add a "container" element to hold both of those to produce valid XML.
 
Share this answer
 
Comments
CPallini 2-May-24 11:14am    
5.
Rob Philpott 3-May-24 7:29am    
I've been doing a bit of this of late actually (JSON=>XML=>JSON). XML was always a hideous thing from the beginning with multiple ways to vaguely express the same thing and a single datatype of 'string'. I see it now more as an indication of the age of a system - it uses XML, it's old, it probably uses WinForms too. Mind you JSON has its faults (again very narrow type system - why oh why no dates?).

I don't mind WinForms actually. It's not pretty by modern standards but it's simple and it works. Sorry, I think I'm waffling. :)
OriginalGriff 3-May-24 8:18am    
I've never liked XML: it's just a poor design IMO.
JSON isn't bad - it's really usable if you need to work between systems / languages but it's limited quite a bit by it's "human readable" side.

What I'd like is a binary format with decent editors / class creators that is cross platform without the weight of a "proper" DB, but ... :sigh:
Rob Philpott 3-May-24 8:22am    
Protocol Buffers?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900