XML-Deserialization of double value with German de

2019-02-21 18:24发布

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

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-21 19:10

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;
              } 
       }
查看更多
时光不老,我们不散
3楼-- · 2019-02-21 19:11

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

查看更多
做自己的国王
4楼-- · 2019-02-21 19:21

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.

查看更多
登录 后发表回答