System.Xml.Serialization.XmlAttributes.XmlDefaultValue Property

Gets or sets the default value of an XML element or attribute.

Syntax

public object XmlDefaultValue { get; set; }

Value

Documentation for this section has not yet been entered.

Remarks

You can specify the default value of an XML element or XML attribute by applying a System.ComponentModel.DefaultValueAttribute to a member. To examine the result of applying the value, compile the application into a DLL or executable, and pass the resulting file as an argument to the XML Schema Definition tool (XSD.exe). In the XML schema document, a default value is assigned to the default attribute. In the following example, the default for the <Animal> element is "Dogs."

Example

<?xml version="1.0"?>
 <schema attributeFormDefault="qualified" 
 elementFormDefault="qualified" targetNamespace="" 
 xmlns="http://www.w3.org/2000/10/XMLSchema">
   <element name="Pets" nullable="true" type="Pets"/>
   <complexType name="Pets">
     <sequence>
       <element default="Dogs" name="Animal" nullable="true" 
        type="string" minOccurs="0"/>
     </sequence>
   </complexType>
 </schema>

To override the default value, create an object and assign it to the XmlAttributes.XmlDefaultValue.

If the value assigned to a field or property is equal to the default value for that field or property, the System.Xml.Serialization.XmlSerializer does not serialize the value to the XML-instance. This is because the assigned value can be recovered from the XML schema. In other words, setting a field or property to its own default value is equivalent of not setting it at all. Likewise, if no value is set for the field or property, the System.Xml.Serialization.XmlSerializer uses the default value found in the schema. In both cases, (setting the property to its default, or not setting it at all), the XML-document instance does not contain any value for the property.

You can use the class constructors instead of the schema to assign the default values. If you are using Xsd.exe to generate a classes from schemas, you can comment out or remove all of the [System.ComponentModel.DefaultValueAttribute("myFieldName")] attributes from the class files.

Requirements

Namespace: System.Xml.Serialization
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0