private string _password;
public string Password
{
get
{
return _password;
}
set
{
if (_password != value)
{
_password = PasswordEncryptor.Encode(value);
OnPropChanged("Password");
}
}
}
PasswordEncryptor
is a class where I call the Encode
method to encode. And after the Password
is encoded, it is serialized by XmlSerializer
to a file in disk. However, every time when the program starts, the file is deserialized, and in set
, PasswordEncryptor.Encode()
encodes the Password
again. Is there a way I can [XmlIgnore]
it ONLY in deserialization?
XmlAttributeOverrides
can help in this scenario.
From MSDN
Allows you to override property, field, and class attributes when you use the XmlSerializer to serialize or deserialize an object
Using this we can make a particular property to be ignored during deserialization.
It will be something like this...
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>));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.