ukerandi
asked on
convert XML to Json C#
Hi Team,
I need to convert following XML results to JSON.also i have used RestSharp
Results
I have used following way but it's not working
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>
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; }
}
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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)
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>
Try this code, generated from online tool - https://app.quicktype.io
Open in new window