Export (0) Print
Expand All

XmlAttributes.XmlDefaultValue Property

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

[Visual Basic]
Public Property XmlDefaultValue As Object
[C#]
public object XmlDefaultValue {get; set;}
[C++]
public: __property Object* get_XmlDefaultValue();
public: __property void set_XmlDefaultValue(Object*);
[JScript]
public function get XmlDefaultValue() : Object;
public function set XmlDefaultValue(Object);

Property Value

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

Remarks

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 example below, the default for the Animal element is "Dogs."

<?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 XmlDefaultValue.

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

Example

[Visual Basic, C#, C++] 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 will treat it as the default value, and not serialize it. If it is set to any other value, the XmlSerializer will serialize the value.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports System.ComponentModel


' This is the class that will be serialized. 
Public Class Pet
    ' The default value for the Animal field is "Dog". 
    <DefaultValueAttribute("Dog")> Public Animal As String 
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideDefaultValue.xml")
        test.DeserializeObject("OverrideDefaultValue.xml")
    End Sub 'Main
    
    
    ' Return an XmlSerializer used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects. 
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()
        
        ' Add an override for the default value of the GroupName. 
        Dim defaultName As Object = "Cat"
        xAttrs.XmlDefaultValue = defaultName
        
        ' Add all the XmlAttributes to the XmlAttribueOverrides object. 
        xOver.Add(GetType(Pet), "Animal", xAttrs)
        
        ' Create the XmlSerializer and return it.
        Return New XmlSerializer(GetType(Pet), xOver)
    End Function
    
    
    Public Sub SerializeObject(ByVal filename As String)
       ' Create an instance of the XmlSerializer class.
       Dim mySerializer As XmlSerializer = CreateOverrider()
        
       ' Writing the file requires a TextWriter.
       Dim writer As New StreamWriter(filename)
        
       ' Create an instance of the class that will be serialized. 
       Dim myPet As 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 = ""
      ' 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)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myPet As Pet = CType(mySerializer.Deserialize(fs), Pet)
        Console.WriteLine(myPet.Animal)
    End Sub
End Class


[C#] 
using System; 
using System.IO; 
using System.Xml; 
using System.Xml.Serialization; 
using System.ComponentModel; 

// This is the class that will be serialized. 
public class Pet 
{
   // The default value for the Animal field is "Dog". 
   [DefaultValueAttribute("Dog")] 
   public string Animal ; 
} 

public class Run 
{ 
   public static void Main() 
   { 
      Run test = new Run();
      test.SerializeObject("OverrideDefaultValue.xml");
      test.DeserializeObject("OverrideDefaultValue.xml"); 
   }
 
   // Return an XmlSerializer used for overriding. 
   public 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= "Cat";
      xAttrs.XmlDefaultValue = defaultAnimal; 

      // Add all the XmlAttributes to the XmlAttribueOverrides object. 
      xOver.Add(typeof(Pet), "Animal", xAttrs); 

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

   public 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= ""; 
      // 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); 
      writer.Close(); 
   } 

   public void DeserializeObject(string filename) 
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Pet myPet= (Pet)mySerializer.Deserialize(fs);
      Console.WriteLine(myPet.Animal);
   }
}


[C++] 
#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". 
public:
   [DefaultValueAttribute(S"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); 
   writer->Close(); 
} 

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

int main() 
{ 
   SerializeObject(S"OverrideDefaultValue.xml");
   DeserializeObject(S"OverrideDefaultValue.xml"); 
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlAttributes Class | XmlAttributes Members | System.Xml.Serialization Namespace

Show:
© 2014 Microsoft