troubleshooting Question

convert XML to Json C#

Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland asked on
XML.NET ProgrammingC#
2 Comments1 Solution45 ViewsLast Modified:
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);
            }





[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>

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);

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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros