由于格式设置,XML反序列化在十进制分析时崩溃 [英] XML deserialization crashes on decimal parse due to formatting
问题描述
当我尝试将XML解析为对象时,抛出System.FormatException。据我所知,这是由于System.Xml.Serialization.XmlSerializer.Deserialize中使用的区域性而导致,期望小数点是点,但xml包含逗号。
I get a System.FormatException thrown when i try to parse XML into an object. As far as I can tell, it's due to the culture used in System.Xml.Serialization.XmlSerializer.Deserialize, wich expects a dot as the decimal character, but the xml contains a comma.
对象看起来如下:
public sealed class Transaction
{
[XmlElement("transactionDate")]
public DateTime TransactionDate { get; set; }
[XmlElement("transactionAmount")]
public decimal Amount { get; set; }
[XmlElement("transactionDescription")]
public string Description { get; set; }
[XmlElement("transactionType")]
public int Type { get; set; }
public static Transaction FromXmlString(string xmlString)
{
var reader = new StringReader(xmlString);
var serializer = new XmlSerializer(typeof(Transaction));
var instance = (Transaction) serializer.Deserialize(reader);
return instance;
}
}
xml:
The xml:
<transaction>
<transactionDate> 2013-07-02 <transactionDate>
<transactionAmount>-459,00</transactionAmount>
<transactionDescription>description</transactionDescription>
<transactionType>1</transactionType>
</transaction>
我通过引入第二个属性来使它起作用,该属性使用自己的文化:
I've made it work by introducing a second property that parses the first using my own culture:
namespace MyNamespace
{
[XmlRoot("transaction"), XmlType("Transaction")]
public sealed class Transaction
{
[XmlElement("transactionDate")]
public DateTime TransactionDate { get; set; }
[XmlElement("transactionAmount")]
public string Amount { get; set; }
public decimal AmountAsDecimal {
get
{
decimal value;
Decimal.TryParse(Amount, NumberStyles.Any, CultureInfo.CreateSpecificCulture("sv-SE"), out value);
return value;
}
}
[XmlElement("transactionDescription")]
public string Description { get; set; }
[XmlElement("transactionType")]
public int Type { get; set; }
public static Transaction FromXmlString(string xmlString)
{
var reader = new StringReader(xmlString);
var serializer = new XmlSerializer(typeof(Transaction));
var instance = (Transaction) serializer.Deserialize(reader);
return instance;
}
}
}
这暴露了我不想要的额外属性。
which exposes an extra property that i don't want there.
所以我的问题是:还有另一种方法可以执行此操作,而无需遍历每个元素并进行解析/手动将其分配给对象?
So my question is: is there another way to do this, without iterating over each element and parsing/assigning it to the object "manually"?
推荐答案
XML序列化器使用标准化的Number和DateTime格式,该标准在W3C模式数据类型规范 http://www.w3.org/TR/xmlschema-2/ 。
XML serializer uses a standardized Number and DateTime format, the standard is defined in the W3C schema datatype specification http://www.w3.org/TR/xmlschema-2/.
不要期望 XmlSerializer
注意线程的 CultureInfo
,它有意使用标准化格式来确保您可以独立于区域性/区域设置进行序列化/反序列化。
Don't expect XmlSerializer
to pay attention to the thread's CultureInfo
, it intentionally uses a standardized format to ensure you can serialize/deserialize independent of the culture/locale.
这篇关于由于格式设置,XML反序列化在十进制分析时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!