We help IT Professionals succeed at work.

convert XML to Json C#

ukerandi
ukerandi asked
on
50 Views
Last Modified: 2020-05-13
Hi Team,

I need to convert following XML results to JSON.also i have used RestSharp
Results
<?xml version="1.0"?>
<table>
 <row id="18" employeeId="20">
  <field id="date">2017-05-15</field>
  <field id="location">test</field>
  <field id="department">Production (Direct)</field>
  <field id="division">abc</field>
  <field id="jobTitle">Stores Material Handler</field>
  <field id="reportsTo">abc cde</field>
 </row>

</table>

Open in new window


I have used following way but it's not working
 public class table
    {
        public row[] row { get; set; }

    }
    public class row
    {
        public string id { get; set; }
        public string employeeId { get; set; }

    }
    public class field
    {
        public string id { get; set; }
        public string Value { get; set; }

    }

Open in new window

Comment
Watch Question

Rikin ShahMicrosoft Dynamics CRM Consultant
CERTIFIED EXPERT

Commented:
Hi,

Try this code, generated from online tool - https://app.quicktype.io

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Temperatures
    {
        [JsonProperty("row", NullValueHandling = NullValueHandling.Ignore)]
        public Row Row { get; set; }
    }

    public partial class Row
    {
        [JsonProperty("@id", NullValueHandling = NullValueHandling.Ignore)]
        public string Id { get; set; }

        [JsonProperty("@employeeId", NullValueHandling = NullValueHandling.Ignore)]
        public string EmployeeId { get; set; }

        [JsonProperty("field", NullValueHandling = NullValueHandling.Ignore)]
        public List<Field> Field { get; set; }
    }

    public partial class Field
    {
        [JsonProperty("@id", NullValueHandling = NullValueHandling.Ignore)]
        public string Id { get; set; }

        [JsonProperty("#text", NullValueHandling = NullValueHandling.Ignore)]
        public string Text { get; set; }
    }
}

Open in new window

Author

Commented:
@ Rikin Shah 
It's not working only EmployeeId  and Id  will get data.
But after calling "Row " class
[code]
  public class Row
[/code]
But this part not working
[code]
public partial class Field
[/code]
I have tried different ways like changing
 public List<Field> Field { get; set; }  to  public Field[] Field { get; set; }

but not working any idea
CERTIFIED EXPERT
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
@ louisfr 
it's looks very close to answer. But problem is it's showing one one records
I think if we can add to Table List it will work not sure how to change this coding to List
[code]
public static List<string> Xml2Json(string xml) {     var x = new XmlSerializer(typeof(Table));     Table table;     using (var r = new StringReader(xml))     {         table = (Table)x.Deserialize(r);     }     var j = new JsonSerializer();     using (var w = new StringWriter())     {         j.Serialize(w, table);         return w.ToString();     } }

[/code]

request details like that(XML)

<?xml version="1.0"?>
<table>
 <row id="18" employeeId="20">
  <field id="date">2017-05-15</field>
  <field id="location">test</field>
  <field id="department">Production (Direct)</field>
  <field id="division">abc</field>
  <field id="jobTitle">Stores Material Handler</field>
  <field id="reportsTo">abc cde</field>
 </row>
 <row id="19" employeeId="20">
  <field id="date">2017-06-15</field>
  <field id="location">test</field>
  <field id="department">Production (Direct)</field>
  <field id="division">abc</field>
  <field id="jobTitle">Stores Material Handler</field>
  <field id="reportsTo">abc cdettt</field>
 </row>
 <row id="20" employeeId="20">
  <field id="date">2017-07-15</field>
  <field id="location">test</field>
  <field id="department">Production (Direct)</field>
  <field id="division">abc</field>
  <field id="jobTitle">Stores Material Handler</field>
  <field id="reportsTo">abc cde111</field>
 </row>
</table>
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.