I want to reproduce this xml structure:
...
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount currencyID="RSD">5000</cbc:LineExtensionAmount>
</cac:LegalMonetaryTotal>
...
And it works when I have non-complex properties in the class LegalMonetaryTotal
, but in order to put XmlAttribute currencyId
I was forced to create instead of decimal field:
[XmlElement(Namespace = Constants.CbcNamespace)]
public decimal? LineExtensionAmount { get; set; }
I created:
[XmlType(Namespace = Constants.CbcNamespace)]
public class LineExtensionAmountDto
{
/// <summary>
/// LineExtensionAmount
/// </summary>
[XmlElement(Namespace = Constants.CbcNamespace)]
public decimal? Amount { get; set; } = 54545;
/// <summary>
/// CurrencyID
/// </summary>
[XmlAttribute("currencyID")]
public string? CurrencyID { get; set; } = "RSD";
}
And I got wrong output:
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount currencyID="RSD">
<cbc:Amount>5000</cbc:Amount>
</cbc:LineExtensionAmount>
</cac:LegalMonetaryTotal>
As you can see, the xml attribute is on the correct spot, but I need to remove Amount
node, and to present the value one level up in, like this:
<cbc:LineExtensionAmount currencyID="RSD"> 1000 </cbc:LineExtensionAmount>
I tried to put [XmlText]
but it can work together with [XmlAttribute]
CodePudding user response:
The XmlText
attribute cannot be applied on nullable value types.
This is the class that will work:
[XmlType(Namespace = Constants.CbcNamespace)]
public class LineExtensionAmountDto
{
[XmlText]
public decimal Amount { get; set; }
[XmlAttribute("currencyID")]
public string? CurrencyID { get; set; }
}
See workaround: https://stackoverflow.com/a/28110988/5045688