我在反序列化 XML 对象时遇到问题。原始 XML 显示 nonexpiredcredits 值为 5,但该对象被反序列化,值为 0。最重要的是,没有遇到异常,反序列化过程似乎只是跳过了该元素。任何帮助,将不胜感激。
原始 XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<theObject>
<mobilecredits>
<nonexpirecredits>5</nonexpirecredits>
</mobilecredits>
</theObject>
物体:
[Serializable()]
[XmlRoot("theObject")]
public class mobilecreditsWrapper
{
[XmlElement("mobilecredits")]
public mobilecredits credits { get; set; }
}
[Serializable()]
public class mobilecredits
{
[XmlElement("nonexpiredcredits")]
public int nonexpiredcredits { get; set; }
}
反序列化片段:
XmlSerializer s = new XmlSerializer(typeof(T));
//T is set to mobilecreditsWrapper in the generic function this code snippet is found in
var sr = new StringReader(res);
try
{
obj = (T)s.Deserialize(sr);
}
catch (Exception ex)
{
//this is not hit
}