Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML-Deserialization of double value with German decimal separator in C#

i'm trying to deserialize a Movie object from a "German" xml string:

string inputString = "<?xml version=\"1.0\"?>"
    + "<movie title=\"Great Bollywood Stuff\">"
    + "<rating>5</rating>"
    + "<price>1,99</price>" // <-- Price with German decimal separator!
    + "</movie>";

XmlSerializer movieSerializer = new XmlSerializer(typeof(Movie));
Movie inputMovie;

using (StringReader sr = new StringReader(inputString))
{
    inputMovie = (Movie)movieSerializer.Deserialize(sr);
}
System.Console.WriteLine(inputMovie);

here the Movie class for reference:

[XmlRoot("movie")]
public class Movie
{

    [XmlAttribute("title")]
    public string Title { get; set; }

    [XmlElement("rating")]
    public int Rating { get; set; }

    [XmlElement("price")]
    public double Price { get; set; }

    public Movie()
    {

    }

    public Movie(string title, int rating, double price)
    {
        this.Title = title;
        this.Rating = rating;
        this.Price = price;
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder("Movie ");
        sb.Append("[Title=").Append(this.Title);
        sb.Append(", Rating=").Append(this.Rating);
        sb.Append(", Price=").Append(this.Price);
        sb.Append("]");

        return sb.ToString();
    }

}

as long i put a the <price> as 1.99 it works perfectly. when i use the German German decimal separator 1,99 it's not working anymore.

please advice

like image 632
Daniel Kutik Avatar asked Dec 11 '11 11:12

Daniel Kutik


3 Answers

In the XML-Schema spec a double/decimal needs to be represented with a . so this behavior is by design.

You could replace the type of Price with string and then have a non-serialized property Realprice that uses a Double.TryParse with an appropriate CultureInfo or NumberFormatInfo.

[XmlRoot("movie")] 
public class Movie {     
      [XmlElement("price")]     
      public string Price { get; set; }  

      [XmlIgnore]
      public double RealPrice { 
         get { 
               double resultprice;
               if (!Double.TryParse(
                  Price, 
                  NumberStyles.Any, 
                  new CultureInfo("de-DE"),
                  resultprice)) throw new ArgumentException("price");
               // resultprice is now parsed, if not an exception is thrown
               return resultprice;
              } 
       }
like image 172
rene Avatar answered Nov 16 '22 00:11

rene


As already noted, that simply isn't a valid way of representing a numeric value in XML. It is fine for a string though. You could do:

[XmlIgnore]
public decimal Price {get;set;}

[XmlElement("price")]
public string PriceFormatted {
    get { return Price.ToString(...); }
    set { Price = decimal.Parse(value, ...); } 
}

Where "..." represents your choice of format specifier and CultureInfo

like image 36
Marc Gravell Avatar answered Nov 16 '22 00:11

Marc Gravell


As mentioned, the XmlSerializer is not the right tool for you, because it is going to use the W3C schema datatype specification.

Alternative solutions include using an XSLT file to convert the input XML before deserialisation, or using another tool such as Linq to XML.

like image 45
devdigital Avatar answered Nov 16 '22 01:11

devdigital