Avatar of ukerandi
ukerandi
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

convert XML to Json C#

Hi Experts,

I working on following XML results. I need to used this code but getting following error message

this code --->table = (List<Table>)x.Deserialize(r);

System.InvalidCastException: 'Unable to cast object of type 'RTabularData.Table' to type
'System.Collections.Generic.List`1[HRTabularData.Table]'.'


 IRestResponse<List<Table>> restResponse = restClient.Get<List<Table>>(request);
            var resultsContent = restResponse.Content;
            doc.LoadXml(resultsContent);
            string json = JsonConvert.SerializeXmlNode(doc);
            var jObject = JObject.Parse(json);
            string test = jObject.GetValue("table").ToString();
            var ss = new JsonSerializer();
            List<Table> table;
            var x = new XmlSerializer(typeof(Table));
            using (var r = new StringReader(resultsContent))
            {
                table = (List<Table>)x.Deserialize(r);
            }

Open in new window






[XmlRoot("table")]
public class Table
{
    public Row row;
}
public class Row
{
    [XmlAttribute]
    public string id;
    [XmlAttribute]
    public string employeeId;
    [XmlElement]
    public Field[] field;
}
public class Field
{
    [XmlAttribute]
    public string id;
    [XmlText]
    public string value;
}

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


I have tried following code.But no error results coming but only one records but not proper way
    IRestResponse<List<Table>> restResponse = restClient.Get<List<Table>>(request);
            var resultsContent = restResponse.Content;
            doc.LoadXml(resultsContent);
            string json = JsonConvert.SerializeXmlNode(doc);
            
            var json1 = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

Open in new window


Results
{{
  "?xml": {
    "@version": "1.0"
  },
  "table": {
    "row": [
      {
        "@id": "18",
        "@employeeId": "20",
        "field": [
          {
            "@id": "date",
            "#text": "2017-05-15"
          },
          {
            "@id": "location",
            "#text": "Road"
          },
          {
            "@id": "department",
            "#text": "Production (Direct)"
          },
          {
            "@id": "division",
            "#text": "Un"
          },
          {
            "@id": "jobTitle",
            "#text": "Stores Material Handler"
          },
          {
            "@id": "reportsTo",
            "#text": "Baker"
          }
        ]
      },
      {
        "@id": "469",
        "@employeeId": "20",
        "field": [
          {
            "@id": "date",
            "#text": "2018-10-01"
          },
          {
            "@id": "location",
            "#text": "Road"
          },
          {
            "@id": "department",
            "#text": "Production (Direct)"
          },
          {
            "@id": "division",
            "#text": "Wolf"
          },
          {
            "@id": "jobTitle",
            "#text": "Production Control"
          },
          {
            "@id": "reportsTo",
            "#text": "Harrison"
          }
        ]
      },
      {
        "@id": "864",
        "@employeeId": "20",
        "field": [
          {
            "@id": "date",
            "#text": "2019-08-01"
          },
          {
            "@id": "location",
            "#text": "abc Road"
          },
          {
            "@id": "department",
            "#text": "Production (Direct)"
          },
          {
            "@id": "division",
            "#text": "rrWolf"
          },
          {
            "@id": "jobTitle",
            "#text": "Production Control - Expeditor"
          },
          {
            "@id": "reportsTo",
            "#text": "te"
          }
        ]
      }
    ]
  }
}}

Any idea much appriciated
.NET ProgrammingC#XML

Avatar of undefined
Last Comment
ukerandi

8/22/2022 - Mon