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

橙三吉。 提交于 2019-12-09 02:59:20

问题


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


回答1:


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




回答2:


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:


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.



来源:https://stackoverflow.com/questions/8463733/xml-deserialization-of-double-value-with-german-decimal-separator-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!