Export (0) Print
Expand All

SoapAttributeAttribute Constructor

Initializes a new instance of the SoapAttributeAttribute class.

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

public SoapAttributeAttribute()

Use this constructor to create a SoapAttributeAttribute when you override the serialization of a class member. Create the SoapAttributeAttribute, set its properties, and set the object to the SoapAttribute property of a SoapAttributes object. For more details, see the SoapAttributeOverrides class overview.

The following example creates a new SoapAttributeAttribute that is used to override the serialization of a field. After creating a SoapAttributeAttribute and setting its properties, the object is set to the SoapAttribute property of a SoapAttributes. The SoapAttributes is then added to a SoapAttributeOverrides that is used to create an XmlSerializer.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Group
   // This attribute will be overridden.
   [SoapAttribute (Namespace = "http://www.cpandl.com")]
   public string GroupName;


public class Run
   public static void Main()
      Run test = new Run();


   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();

      // Set the object properties.
      myGroup.GroupName = ".NET";

      // Serialize the class, and close the TextWriter.
      overRideSerializer.Serialize(writer, myGroup);


   private XmlSerializer CreateOverrideSerializer(){
      SoapAttributeOverrides mySoapAttributeOverrides = 
		new SoapAttributeOverrides();
      SoapAttributes mySoapAttributes = new SoapAttributes();
      // Create a new SoapAttributeAttribute to override the 
      // one applied to the Group class. The resulting XML 
      // stream will use the new namespace and attribute name.
      SoapAttributeAttribute mySoapAttribute = 
      new SoapAttributeAttribute();
      mySoapAttribute.AttributeName = "TeamName";
      // Change the Namespace.
      mySoapAttribute.Namespace = "http://www.cohowinery.com";

      mySoapAttributes.SoapAttribute = mySoapAttribute;
      Add(typeof(Group), "GroupName" ,mySoapAttributes);
      XmlTypeMapping myMapping = (new SoapReflectionImporter
      XmlSerializer ser = new XmlSerializer(myMapping);
      return ser;

//<?xml version="1.0" encoding="utf-8" ?> 
// <Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
//xmlns:xsd="http://www.w3.org/2001/XMLSchema" n1:TeamName=".NET" 
//xmlns:n1="http://www.cohowinery" /> 

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

© 2015 Microsoft