当某些属性有时可用时尝试序列化项目,有时则不然。问题是错误地我将VerifyMinAmount作为bool,因为这个XmlDeserialize抛出了异常。现在已修复
<items>
<item idimagelogo="892" verifyamount="True" receipt="02" >
</item>
<item idimagelogo="893" Receipt="02" >
</item>
<item idimagelogo="893" verifyamount="True" >
</item>
</items>
[XmlRoot(ElementName = "item")]
public class Item
{
[XmlAttribute(AttributeName = "idimagelogo")]
public string idimagelogo { get; set; }
[XmlAttribute(AttributeName = "verifyamount")]
public string VerifyMinAmount { get; set; }
[XmlAttribute(AttributeName = "receipt")]
public string Receipt { get; set; }
}
public static T XmlDeserialize<T>(XNode data,string rootAttribute = "") where T : class, new()
{
if (data == null) {
return null;
}
XmlSerializer ser=null;
if(String.IsNullOrEmpty(rootAttribute))
ser = new XmlSerializer(typeof (T));
else
ser = new XmlSerializer(typeof(T), new XmlRootAttribute(rootAttribute));
return (T) ser.Deserialize(data.CreateReader());
}