Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XmlAttributes.XmlDefaultValue Property

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

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

public function get XmlDefaultValue () : Object 
public function set XmlDefaultValue (value : Object)

Property Value

Type: System.Object
An Object that represents the default value of an XML element or attribute.

You can specify the default value of an XML element or XML attribute by applying a 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."

<?xml version="1.0"?>
 <schema attributeFormDefault="qualified" 
 elementFormDefault="qualified" targetNamespace="" 
   <element name="Pets" nullable="true" type="Pets"/>
   <complexType name="Pets">
       <element default="Dogs" name="Animal" nullable="true" 
        type="string" minOccurs="0"/>

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

If the value assigned to a field or property is equal to the default value for that field or property, the 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 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.

The following example shows a class named Pet that contains a field that has a default value set to "Dog". However, the example also creates an XmlAttributes object, and sets its XmlDefaultValue property to a new default value ("Cat"). This overrides the original default value. Thus, if the field value is set to "Cat", the XmlSerializer treats it as the default value, and not serialize it. If it is set to any other value, the XmlSerializer serializes the value.

No code example is currently available or this language may not be supported.
#using <mscorlib.dll>
#using <System.dll>
#using <System.Xml.dll>
using namespace System; 
using namespace System::IO; 
using namespace System::Xml; 
using namespace System::Xml::Serialization; 
using namespace System::ComponentModel; 

// This is the class that will be serialized. 
public __gc class Pet 
   // The default value for the Animal field is "Dog". 
   String* Animal ; 

// Return an XmlSerializer used for overriding. 
XmlSerializer* CreateOverrider() 
   // Create the XmlAttributeOverrides and XmlAttributes objects. 
   XmlAttributeOverrides* xOver = new XmlAttributeOverrides(); 
   XmlAttributes* xAttrs = new XmlAttributes(); 

   // Add an override for the default value of the GroupName. 
   Object* defaultAnimal= S"Cat";
   xAttrs->XmlDefaultValue = defaultAnimal; 

   // Add all the XmlAttributes to the XmlAttribueOverrides object. 
   xOver->Add(__typeof(Pet), S"Animal", xAttrs); 

   // Create the XmlSerializer and return it.
   return new XmlSerializer(__typeof(Pet), xOver);

void SerializeObject(String* filename)
   // Create an instance of the XmlSerializer class.
   XmlSerializer* mySerializer = CreateOverrider(); 

   // Writing the file requires a TextWriter.
   TextWriter* writer = new StreamWriter(filename); 

   // Create an instance of the class that will be serialized. 
   Pet* myPet = new Pet(); 

   /* Set the Animal property. If you set it to the default value,
   which is "Cat" (the value assigned to the XmlDefaultValue
   of the XmlAttributes object), no value will be serialized.
   If you change the value to any other value (including "Dog"),
   the value will be serialized.
   // The default value "Cat" will be assigned (nothing serialized).
   myPet->Animal= S""; 
   // Uncommenting the next line also results in the default 
   // value because Cat is the default value (not serialized).
   //  myPet.Animal = "Cat"; 

   // Uncomment the next line to see the value serialized:
   // myPet.Animal = "fish";
   // This will also be serialized because Dog is not the 
   // default anymore.
   //  myPet.Animal = "Dog";
   // Serialize the class, and close the TextWriter. 
   mySerializer->Serialize(writer, myPet); 

void DeserializeObject(String* filename) 
   XmlSerializer* mySerializer = CreateOverrider();
   FileStream* fs = new FileStream(filename, FileMode::Open);
   Pet* myPet= dynamic_cast<Pet*>(mySerializer->Deserialize(fs));

int main() 

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

© 2015 Microsoft