question

Tutumon-5032 avatar image
0 Votes"
Tutumon-5032 asked DuaneArnold-0443 edited

Deserializing XML with negative deicmal value

I have a problem in reserializing an XML file into C#. XML Looks like below

<?xml version='1.0' encoding='UTF-8'?>
<Report_Data>
<Report_Entry>
<CustomerId>CST100</CustomerId>
<TotalAmount>-210.34</TotalAmount>
</Report_Entry>
<Report_Entry>
<CustomerId>CST101</CustomerId>
<TotalAmount>-50.31</TotalAmount>
</Report_Entry>
</Report_Data>
POCO Class

[XmlRoot(ElementName = "Report_Data")]
public class CustomerInvoiceList
{
[XmlElement("Report_Entry")]
public List<ReportEntry> ReportEntry { get; set; }
}

[Serializable]
public class ReportEntry
{
[XmlElement(ElementName = "CustomerId")]
public string CustomerId{ get; set; }

     [XmlElement(ElementName = "TotalAmount")]
     public string TotalAmount{ get; set; }
 }

//function to parse the input xml
public CustomerInvoiceList Parse(string inputData)
{
try
{
CustomerInvoiceList parsedResult;

             var serializer = new XmlSerializer(typeof(CustomerInvoiceList));
             using (TextReader reader = new StringReader(inputData))
             {
                 parsedResult = (CustomerInvoiceList)serializer.Deserialize(reader);
             }
             return parsedResult;
         }
         catch (Exception ex)
         {
             throw ex;
         }

When i have this input, the TotalAmount filed contains data like (50.31), after the deserialization. How do i get the decimal value including -, after the deserialization process.

Thanks
Tutu

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


I think that TotalAmount should be "-50.31" and "-210.34". Are you sure that you read the right data?


0 Votes 0 ·

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 edited

XML object serialization back to .NET object that has public properties, the property type can be primitive type numeric decimal, which would be converted back into a primitive numeric decimal type from XML that is a string representation of a decimal data.

The XML data exchange is string data. A .NET object/class that is using public properties using primitive numeric types such as int, double, decimal, etc., and ect. is converted to XML that is a string representation of the data in the public properties of the .NET object that is XML serialized. But when the XML object that represents the properties of the .NET object is converted back to a .NET object, the conversion of the XML string data is converted back to primitive type decimal, int, double defined by the public property in the the .NET object.

The property should be primitive numeric decimal type, the XML object conversion back into a .NET object will take care of the conversion.

[XmlElement(ElementName = "TotalAmount")]
public decimal TotalAmount{ get; set; }

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.