private string _password;
public string Password
{
get
{
return _password;
}
set
{
if (_password != value)
{
_password = PasswordEncryptor.Encode(value);
OnPropChanged("Password");
}
}
}PasswordEncryptor是一个类,我在其中调用Encode方法进行编码。在对Password进行编码之后,XmlSerializer将其序列化为磁盘中的一个文件。但是,每次程序启动时,文件都被反序列化,在set中,PasswordEncryptor.Encode()再次对Password进行编码。是否有一种仅在反序列化中可以[XmlIgnore]的方法?
发布于 2014-09-24 19:01:17
在这种情况下,XmlAttributeOverrides可以提供帮助。
来自MSDN
允许您在使用XmlSerializer序列化或反序列化对象时覆盖属性、字段和类属性。
使用它,我们可以在反序列化过程中使特定的属性被忽略。
会是这样的..。
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "<elementName>";
XmlAttributes attrs = new XmlAttributes();
attrs.XmlIgnore = true;
attrs.XmlElements.Add(attr);
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
attrOverrides.Add(typeof(<className>), "<elementName>", attrs);
// use this when deserializing
XmlSerializer s = new XmlSerializer(typeof(<className>), attrOverrides);
// use this when serializing
XmlSerializer s = new XmlSerializer(typeof(<className>));https://stackoverflow.com/questions/26024304
复制相似问题