样本 Xml:
<TestXML>
<TestDecimal attr1="Required" attr2="false">0.34</TestDecimal>
<TestInteger attr1="MyAttr" attr2="1" DateAdded="">25</TestInteger>
</TestXML>
类定义:
public class TestXML {
public DecimalValue TestDecimal;
public IntegerValue TestInteger;
}
public class IntegerValue {
[XmlText]
public int value {get; set;}
[XmlAnyAttribute]
public XmlAttribute[] XAttributes {get; set;}
}
public class DecimalValue {
[XmlText]
public decimal value {get; set;}
[XmlAnyAttribute]
public XmlAttribute[] XAttributes {get; set;}
}
反序列化代码:
XmlSerializer serializer = new XmlSerializer(typeof(TestXML));
using (FileStream fs = new FileStream(@"C:\Test.xml", FileMode.Open)) {
TestXML myxml = (TestXML)serializer.Deserialize(fs);
}
在测试中,它返回整数值而不是十进制值:
myxml
TestDecimal
value | 0
XAttributes {string[2]}
[0] Name="attr1", Value="Required"
[1] Name="attr2", Value="false"
TestInteger
value | 25
XAttributes {string[3]}
[0] Name="attr1", Value="MyAttr"
[1] Name="attr2", Value="1"
[2] Name="DateAdded", Value=""
反序列化小数是否会导致它返回为 0?使用原始类型的小数,十进制值可以正常返回。