.NET XML Serialization, possibly to use a different method name than PropertySpecified for ignoring

Posted by Lasse V. Karlsen on Stack Overflow See other posts from Stack Overflow or by Lasse V. Karlsen
Published on 2010-06-09T07:39:44Z Indexed on 2010/06/09 8:22 UTC
Read the original article Hit count: 232

I have a bunch of classes that I intend to serialize in order to transport over a webservice call.

These classes already have properties that return whether a given "real" property has a value or not, that is ingrained in a lot of code of our product.

Is it possible, for instance through attributes, for me to specify that each "real" property should be included if a specified other property returns true, but not one that is called RealPropertyNameSpecified?

ie. for instance if I have this class:

[XmlType("test")]
public class TestClass
{
    [XmlIgnore]
    public bool NameHasGotAValue { get; set; }

    [XmlElement("name")]
    public string Name { get; set; }
}

Is it possible for me to use the NameHasGotAValue as the "*Specified" method for the Name property, or is my only option to either rename NameHasGotAValue, add a NameSpecified method, or implement IXmlSerializable?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about xml-serialization