Deserializing XML with negative deicmal value

Tutumon 16 Reputation points
2021-06-06T00:30:36.41+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,418 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-06-06T01:01:31.383+00:00

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.