I am working in project for fun where, I have to deserialize my XML below:
<?xml version="1.0" encoding="utf-8"?>
<Remote Created="2022-09-13T09:45:05" Version="1.0.20.2.6.5" SerialNumber="2081" Site="Site" InstallationDate="0001-01-01T00:00:00">
<Setup File="Qwerty" Name="Ytrewq" Encoding="Win32" Decimals="2">
<Transaction SequenceNumber="1000609" StartTime="2022-09-13T09:42:52" EndTime="2022-09-13T09:45:05" Total="3.85" ExpectedTotal="0.00">
<Identities>
<Ident Name="Box" Value="1" InHierarchy="yes" />
<Ident Name="Area" Value="1" InHierarchy="yes" />
</Identities>
<Charges Reject="0" Total="3.85">
<Charge Name="Reject" Label="R" Value="0" Count="0" Total="0.00" />
<Charge Name="10 counts" Label="" Value="10" Count="1" Total="0.10" />
<Charge Name="20 counts" Label="" Value="20" Count="1" Total="0.20" />
<Charge Name="50 counts" Label="" Value="50" Count="1" Total="0.50" />
<Charge Name="1 counts" Label="" Value="100" Count="1" Total="1.00" />
<Charge Name="2 counts" Label="" Value="200" Count="1" Total="2.00" />
<Charge Name="5 counts" Label="" Value="5" Count="1" Total="0.05" />
</Charges >
</Transaction>
</Setup>
</Remote>
And this is my object:
[XmlRoot("Remote")]
public class Remote
{
//Remote
[XmlAttribute(AttributeName = "Created")]
public string Created { get; set; }
[XmlAttribute(AttributeName = "Version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "SerialNumber")]
public string SerialNumber { get; set; }
[XmlAttribute(AttributeName = "Site")]
public string Site { get; set; }
[XmlAttribute(AttributeName = "InstallationDate")]
public DateTime InstallationDate { get; set; }
//Setup
[XmlElement(ElementName = "File")]
public string File { get; set; }
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Encoding")]
public string Encoding { get; set; }
[XmlElement(ElementName = "Decimals")]
public int Decimals { get; set; }
//Transaction
[XmlAttribute(AttributeName = "SequenceNumber")]
public int SequenceNumber { get; set; }
[XmlAttribute(AttributeName = "StartTime")]
public DateTime StartTime { get; set; }
[XmlAttribute(AttributeName = "EndTime")]
public DateTime EndTime { get; set; }
[XmlAttribute(AttributeName = "Total")]
public double Total { get; set; }
[XmlAttribute(AttributeName = "ExpectedTotal")]
public double ExpectedTotal { get; set; }
[XmlArrayItem(ElementName = "Identities")]
public List<Ident> Identities { get; set; } = new List<Ident> { };
[XmlArrayItem(ElementName = "Charges")]
public List<Charges> Charges{ get; set; } = new List<Charges> { };
[XmlAttribute(AttributeName = "Reject")]
public string Reject { get; set; }
[XmlType("Charge")]
public class Charges
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Label")]
public string Label { get; set; }
[XmlAttribute(AttributeName = "Value")]
public double Value { get; set; }
[XmlAttribute(AttributeName = "Count")]
public int Count { get; set; }
[XmlAttribute(AttributeName = "Total")]
public double Total { get; set; }
}
[XmlType("Ident")]
public class Ident
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Value")]
public int Value { get; set; }
[XmlAttribute(AttributeName = "InHierarchy")]
public string InHierarchy { get; set; }
}
}
After that, I have to serialize to JSON, which is working partially, and I got this result:
{
"Created": "2022-09-13T09:45:05",
"Version": "1.17.1460",
"SerialNumber": "2081",
"Site": "Site",
"InstallationDate": "0001-01-01T00:00:00",
"File": null,
"Name": null,
"Encoding": null,
"Decimals": 0,
"SequenceNumber": 0,
"StartTime": "0001-01-01T00:00:00",
"EndTime": "0001-01-01T00:00:00",
"Total": 0.0,
"ExpectedTotal": 0.0,
"Identities": [],
"Charges": [],
"Reject": null
}
I'm not sure what could be wrong on the way I map this, but some values just return as null, and the arrays returns with no data. Everything below the 'File' is not workin properly. Tried to another types of elements/attirbutes and they didn't work. I'm quite new with C#, so I'm not sure what to do. Can someoone help me with that?
Cheers.