Export (0) Print
Expand All

SoapAttributes.SoapDefaultValue 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 Object SoapDefaultValue { get; set; }

Property Value

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

The default value of a member can be set by applying a DefaultValueAttribute attribute to the member. If the member is being serialized as an encoded SOAP message, you can change the default value by creating a new DefaultValueAttribute, setting its Value property, and setting the SoapDefaultValue property to the object. Add the SoapAttributes to a SoapAttributeOverrides. For more details, see the SoapAttributeOverrides class overview.

The following example serializes a class named Group that includes a field named GroupName. The default value is set with the DefaultValueAttribute to ".NET". By either not setting the field, or by setting it to ".NET", the value is not serialized (because the default value is already known). The sample also overrides the default value in the CreateOverrideSerializer method, which is called by the SerializeOverride method. The example calls both methods, SerializeOriginal and SerializeOverride, and sets the same value (".NET") for the GroupName field. Because of the override, the value is serialized only when calling the SerializeOverride method.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.ComponentModel;

public class Group
{
   // The default is set to .NET.
   [DefaultValue(".NET")]
   public string GroupName;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeOriginal("SoapOriginal.xml");
      test.SerializeOverride("mySoapAttributeOverridesideAttributes.xml");
      test.DeserializeOriginal("SoapOriginal.xml");
      test.DeserializeOverride("mySoapAttributeOverridesideAttributes.xml");
   }
   public void SerializeOriginal(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  
      new XmlSerializer(typeof(Group));

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

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Setting the GroupName to '.NET' is like not setting it at all 
      // because it is the default value. So no value will be  
      // serialized, and on deserialization it will appear as a blank.
      myGroup.GroupName = ".NET";

      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
      writer.Close();
   }

   public void SerializeOverride(string filename)
   {
      // Create an instance of the XmlSerializer class 
      // that overrides the serialization.
      XmlSerializer overRideSerializer = CreateOverrideSerializer();

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

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // The override specifies that the default value is now  
      // 'Team1'. So setting the GroupName to '.NET' means 
      // the value will be serialized.
      myGroup.GroupName = ".NET";
      // Serialize the class, and close the TextWriter.
      overRideSerializer.Serialize(writer, myGroup);
       writer.Close();

   }


   public void DeserializeOriginal(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer= new XmlSerializer(typeof(Group));
      // Reading the file requires a TextReader.
      TextReader reader = new StreamReader(filename);

      // Deserialize and cast the object.
      Group myGroup; 
      myGroup = (Group) mySerializer.Deserialize(reader);

      Console.WriteLine(myGroup.GroupName);
      Console.WriteLine();
   }

   public void DeserializeOverride(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer overRideSerializer = CreateOverrideSerializer();
      // Reading the file requires a TextReader.
      TextReader reader = new StreamReader(filename);

      // Deserialize and cast the object.
      Group myGroup; 
      myGroup = (Group) overRideSerializer.Deserialize(reader);

      Console.WriteLine(myGroup.GroupName);

   }

   private XmlSerializer CreateOverrideSerializer()
   {
      SoapAttributeOverrides mySoapAttributeOverrides = 
      new SoapAttributeOverrides();
      SoapAttributes soapAtts = new SoapAttributes();
      // Create a new DefaultValueAttribute object for the GroupName 
      // property.
      DefaultValueAttribute newDefault = 
      new DefaultValueAttribute("Team1");
      soapAtts.SoapDefaultValue = newDefault;

      mySoapAttributeOverrides.Add(typeof(Group), "GroupName", 
      soapAtts);

      // Create an XmlTypeMapping that is used to create an instance  
      // of the XmlSerializer. Then return the XmlSerializer object.
      XmlTypeMapping myMapping = (new SoapReflectionImporter(
      mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));
	
      XmlSerializer ser = new XmlSerializer(myMapping);
      return ser;
   }

}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft