Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON Array to XML in C#

All, I know this question has been asked multiple times, and I feel like I have looked at every single one of them on here! I have a C# Winforms application that is making a call to an outside webhook, pulling in JSON formatted data. I need to take this data and convert it to a data table, with the end result imported into a database. The thing I am stuck on, and have been for the last day and a half, is trying to parse the JSON data into XML.

Here is the JSON data (example) I am getting from the webhook:

 [
  ["Item Title",
   "Bidder Name",
   "Bidder Email",
   "Bidder Phone Number",
   "Bidder Username",
   "Bid Amount",
   "Bid Time",
   "Operation",
   "Auto Bid Amount",
   "Bidder Address",
   "Bidder City",
   "Bidder State",
   "Bidder Country"
  ],
  ["Test item 1(#)"],
  ["",
   "Tom Kelly",
   "[email protected]",
   "6303278300",
   "testaccount",
   "50.0",
   "07/09/2016 07:17 PM CDT",
   "Bid Amount",
   null,
   null,
   null,
   null,
  "US"
  ]
]  

At first I tried using the DeserializeXMLNode function, but that didn't work.

XmlNode xml = JsonConvert.DeserializeXmlNode(body, "BiddingHistory");

Then I thought that I would use the DeserializeObject function, but again, not working.

var jRst = JsonConvert.DeserializeObject(body);

When I use the DeserializeObject function I am getting the following result:

    {[
  [
    "Item Title",
    "Bidder Name",
    "Bidder Email",
    "Bidder Phone Number",
    "Bidder Username",
    "Bid Amount",
    "Bid Time",
    "Operation",
    "Auto Bid Amount",
    "Bidder Address",
    "Bidder City",
    "Bidder State",
    "Bidder Country"
  ],
  [
    "Test Item 1"
  ],
  [
    "",
    "Tom Kelly",
    "[email protected]",
    "6303278300",
    "testaccount",
    "75.0",
    "07/30/2016 06:14 PM CDT",
    "Bid Amount",
    null,
    null,
    null,
    null,
    "US"
  ]
]}

After converting the object to a string, I ran this through the XMLNodeConverter, and it was failing on the "#" character, so I did a string replace and took out that character (wasn't needed anyway and I do not have control of what is coming to me in the JSON data) then ran my code again.

XmlNode xml = JsonConvert.DeserializeXmlNode(sBody, "BiddingHistory");

So now what I am getting is this error:

XmlNodeConverter can only convert JSON that begins with an object

Would someone please steer me in the right direction here? I'm thinking this is a simple thing to do and I am overcomplicating it.

Thanks.

like image 957
DadTo2 Avatar asked Mar 12 '23 13:03

DadTo2


2 Answers

You are getting an error because your JSON data is an array and what you have done is:

XmlNode xml = JsonConvert.DeserializeXmlNode(sBody, "BiddingHistory");

the above line of code will only work for JSON objects.

So, if your JSON is an Array, then try this:

XmlNode xml = JsonConvert.DeserializeXmlNode("{\"Row\":" + sBody + "}", "BiddingHistory").ToXmlString();
like image 186
Raktim Biswas Avatar answered Mar 15 '23 22:03

Raktim Biswas


Use service stack from nuget Service Stack

add reference to your program

using ServiceStack;

Convert your json to object

var jRst = JsonConvert.DeserializeObject(body);

after that you can get xml using service stack like below

var xml = jRst.ToXml();
like image 22
Mostafiz Avatar answered Mar 15 '23 23:03

Mostafiz