0

How can I implement XML similar to what I did with Json below:

I overridden JsonConverter class of Newtonsoft

public class DecimalFormatConverter : JsonConverter 
{ 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
        writer.WriteValue(string.Format("{0:N2}", value)); 
    } 
}

And add attribute for Total:

JsonConverter(typeof(DecimalFormatConverter))] 
public decimal Total { get; set; }

Also, Where can I find a similar XMLConverter class to reuse? Newtonsoft only has JsonConverter

nozzleman
  • 9,529
  • 4
  • 37
  • 58
Asknap
  • 1
  • If you're talking about `XmlSerializer`, there's no direct analogue of `JsonConverter`. One possibility is to use a surrogate type for your `decimal` property along the lines of [Most elegant xml serialization of Color structure](http://stackoverflow.com/a/4322461/3744182). Another would be to [implement `IXmlSerializable`](http://www.codeproject.com/Articles/43237/How-to-Implement-IXmlSerializable-Correctly). Beyond that, please [edit] your question to include a specific [mcve] that shows your precise problem. See also https://stackoverflow.com/help/how-to-ask – dbc Dec 02 '16 at 06:15
  • But if you're just looking to control the formatting for a specific decimal, you could use a surrogate property along the lines of [this answer](http://stackoverflow.com/a/1118855/3744182) or [this one](http://stackoverflow.com/a/1613537/3744182). – dbc Dec 02 '16 at 22:49

0 Answers0