4

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",
   "tomkelly7630@gmail.com",
   "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",
    "tomkelly7630@gmail.com",
    "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.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
DadTo2
  • 69
  • 1
  • 7

2 Answers2

5

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();
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
  • Rakitić, I'm attempting this to see if it works, but am coming up with an error on my build. Error is: System.Xml.XmlDocument does not contain a definition for 'ToXmlString' and no extension method 'ToXmlString' accepting a first argument of type 'System.XmlDocument' could be found. I do have using System.Xml and using Newtonsoft.Json in my code. Anything else I might be missing here to try this out? – DadTo2 Jul 31 '16 at 18:56
  • Sorry, hit enter too quickly. Error above. – DadTo2 Jul 31 '16 at 19:00
  • you must have declared `XmlDocument xmlDoc = new XmlDocument();` somewhere in your code...try replacing `XmlDocument` to `XDocument`. – Raktim Biswas Jul 31 '16 at 19:04
  • I did have that exact line five lines after inserting your suggestion, so I've taken that out, rebuilt and still have the same error. Here is the code I have with your line and the next three lines after: XmlNode xml = JsonConvert.DeserializeXmlNode("{\"Row\":" + sbody + "}", "BiddingHistory").ToXmlString(); var xmlReader = new XmlNodeReader(xml); DataSet dataSet = new DataSet(); dataSet.ReadXml(xmlReader); – DadTo2 Jul 31 '16 at 19:22
  • did you use the namespace `System.Xml.LINQ;`? – Raktim Biswas Jul 31 '16 at 19:46
  • Yes, I did. I also just tried it again, but without the .ToXmlString() and it ran, but the xml it gave came out to this (snipped for length): Item TitleBidder NameBidder EmailBidder Phone NumberBidder UsernameBid AmountBid TimeOperationAuto Bid AmountBidder AddressBidder CityBidder StateBidder CountryTest Item 2Tom Kellytomkelly7630@gmail.com6303278300testaccount75.0 – DadTo2 Jul 31 '16 at 20:10
  • 1
    It's been a long weekend on this project. I just realized that what you suggested (with just very minor tweaking) worked and the JSON is now in XML format. Appreciate the help Rakitić. Now on to getting this dataset populated. Thanks again all! – DadTo2 Jul 31 '16 at 20:30
2

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();
Mostafiz
  • 7,243
  • 3
  • 28
  • 42