Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

convert XML to Json C#

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

Avatar of Rikin Shah
Rikin Shah
Flag of India image

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

Avatar of ukerandi

ASKER

@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
ASKER CERTIFIED SOLUTION
Avatar of louisfr
louisfr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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>

Open in new window