When using XML serialization to serialize a class, how to make some attribute be outputted conditionally. i.e. In some case, it output this attribute, in other cases, it does not.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can create an additional property which is called
MyPropertySpecified, which returns a boolean.When this property returns true, the
MyPropertyproperty will be serialized. When it returns false, it will not be serialized.Also, you’d want to decorate that property with the
XmlIgnoreAttribute, so that this specific property is not serialized.Example: