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
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