The purpose of the {propertyName}Specified template is described in XML Schema Binding Support: MinOccurs Attribute Binding Support . It has been added to support the XSD schema element, in which:
- The
<element> . - minOccurs is zero.
- The maxOccurs attribute specifies a single instance.
- The data type is converted to a value type.
In this case, xsd.exe /classes will automatically generate (or you can manually generate) a property with the same name as the schema element, and a {propertyName}Specified boolean get / set property that keeps track of whether the element was encountered in XML , and must be serialized back to XML. If an element occurs, {propertyName}Specified set to true , otherwise false . In this way, the deserialized instance can determine whether the property was canceled or its default value was explicitly set during deserialization.
The reverse is also implemented to generate the circuit. If you define a C # type with a couple of properties matching the pattern above, use xsd.exe to create the corresponding XSD file, the corresponding minOccurrs will be added to the schema. For example, the following type is specified:
public class ExampleClass { [XmlElement] public decimal Something { get; set; } [XmlIgnore] public bool SomethingSpecified { get; set; } }
The following scheme will be created and vice versa:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="ExampleClass" nillable="true" type="ExampleClass" /> <xs:complexType name="ExampleClass"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="Something" type="xs:decimal" /> </xs:sequence> </xs:complexType> </xs:schema>
Note that although xsd.exe is only documented to automatically create the {propertyName}Specified for value type properties, XmlSerializer will respect the pattern when used manually for properties of a reference type.
You may ask why xsd.exe not associated with a null value in this case? Perhaps because:
You need to know this template because xsd.exe sometimes generates it for you automatically, however, the interaction between the property and the Specified property is strange and capable of creating errors. You can populate all the properties in your class and then serialize the XML and lose everything, because you also did not set the corresponding Specified properties to true . This "gotcha" appears here from time to time here, see, for example, this question or this one too .
Another “obtained” with this template is that if you need to serialize your type using a serializer that does not support this template, you can > manually disable the output of this property during serialization, and it will probably be necessary to manually set it during deserialization. Since each serializer may have its own mechanism for suppressing properties (or no mechanism at all!), This can become more and more burdensome over time.
(Finally, I'm a little surprised that your MyPropertySpecified runs successfully without a setter. I seem to recall a version of .Net 2.0 in which the missing {propertyName}Specified installer can throw an exception. It no longer plays in later versions, and I don't have 2.0 for testing. So this may be the third question.)
Support for the ShouldSerialize{PropertyName}() method is documented in Properties in Windows Forms Controls: Defining Default Values Using the ShouldSerialize and Reset Methods. As you can see, the documentation is located in the Windows Forms section of MSDN, and not in the XmlSerializer section, so this is essentially a semi-closed functionality. I do not know why support for this method and the Specified property exist in the XmlSerializer . ShouldSerialize was introduced in .NET 1.1 , and I believe that MinOccurs binding support was added in .NET 2.0 , so maybe the earlier functionality didn't quite meet the needs (or taste) of the xsd.exe development xsd.exe ?
Since this is a method, not a property, it lacks the "gotchas" template {propertyName}Specified . It also seems more popular in practice and has been adopted by other serializers, including:
So which template to use?
If xsd.exe automatically generates the {propertyName}Specified , or your type should monitor whether or not a particular element has appeared in the XML file, or if you need an XSD auto-generator to indicate that a specific value is optional, use this template and watch for "gotchas".
Otherwise, use the template ShouldSerialize{PropertyName}() . It has fewer errors and can be more widely supported.