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",
   "[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.

3
  • tried this XmlDocument xdoc = JsonConvert.DeserializeXmlNode(yourjson);? Commented Jul 31, 2016 at 17:52
  • can you share the model class for the json ? I can try then Commented Jul 31, 2016 at 18:03
  • See more info on JSON objects and JSON Arrays Commented Jul 31, 2016 at 18:14

2 Answers 2

6

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();
Sign up to request clarification or add additional context in comments.

7 Comments

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?
Sorry, hit enter too quickly. Error above.
you must have declared XmlDocument xmlDoc = new XmlDocument(); somewhere in your code...try replacing XmlDocument to XDocument.
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);
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!
|
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.