SoapAttributeAttribute Constructors

Definition

Initializes a new instance of the SoapAttributeAttribute class.

Overloads

SoapAttributeAttribute()

Initializes a new instance of the SoapAttributeAttribute class.

SoapAttributeAttribute(String)

Initializes a new instance of the SoapAttributeAttribute class using the specified value as the name of the XML attribute.

SoapAttributeAttribute()

Source:
SoapAttributeAttribute.cs
Source:
SoapAttributeAttribute.cs
Source:
SoapAttributeAttribute.cs

Initializes a new instance of the SoapAttributeAttribute class.

C#
public SoapAttributeAttribute();

Examples

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.

C#
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();
      test.SerializeOverride("SoapOveride.xml");
   }

   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);
       writer.Close();
   }

   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;
      mySoapAttributeOverrides.
      Add(typeof(Group), "GroupName" ,mySoapAttributes);
    
      XmlTypeMapping myMapping = (new SoapReflectionImporter
        (mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));
    
      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" />

Remarks

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.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

SoapAttributeAttribute(String)

Source:
SoapAttributeAttribute.cs
Source:
SoapAttributeAttribute.cs
Source:
SoapAttributeAttribute.cs

Initializes a new instance of the SoapAttributeAttribute class using the specified value as the name of the XML attribute.

C#
public SoapAttributeAttribute(string attributeName);
C#
public SoapAttributeAttribute(string attrName);

Parameters

attributeNameattrName
String

The name of the XML attribute.

Examples

The following example serializes a class that contains several fields to which a SoapAttributeAttribute is applied.

C#
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

[SoapInclude(typeof(Vehicle))]
public class Group
{
   [SoapAttribute (Namespace = "http://www.cpandl.com")]
   public string GroupName;

   [SoapAttribute(DataType = "base64Binary")]
   public Byte [] GroupNumber;

   [SoapAttribute(DataType = "date", AttributeName = "CreationDate")]
   public DateTime Today;
   [SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")]
   public string PostitiveInt;

   public Vehicle GroupVehicle;
}

public class Vehicle
{
   public string licenseNumber;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("SoapAtts.xml");
      test.DeserializeObject("SoapAtts.xml");
   }
   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class that
      // can generate encoded SOAP messages.
      XmlSerializer mySerializer =  ReturnSOAPSerializer();

      Group myGroup=MakeGroup();
      // Writing the file requires a TextWriter.
      XmlTextWriter writer =
      new XmlTextWriter(filename, Encoding.UTF8);
      writer.Formatting = Formatting.Indented;
      writer.WriteStartElement("wrapper");
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
      writer.WriteEndElement();
      writer.Close();
   }

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

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

      Byte [] hexByte = new Byte[2]{Convert.ToByte(100),
      Convert.ToByte(50)};
      myGroup.GroupNumber = hexByte;

      DateTime myDate = new DateTime(2002,5,2);
      myGroup.Today = myDate;
      myGroup.PostitiveInt= "10000";
      myGroup.GroupVehicle = new Vehicle();
      myGroup.GroupVehicle.licenseNumber="1234";
      return myGroup;
   }   	

   public void DeserializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class that
      // can generate encoded SOAP messages.
      XmlSerializer mySerializer =  ReturnSOAPSerializer();

      // Reading the file requires an  XmlTextReader.
      XmlTextReader reader=
      new XmlTextReader(filename);
      reader.ReadStartElement("wrapper");

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

   private XmlSerializer ReturnSOAPSerializer(){
      // Create an instance of the XmlSerializer class.
      XmlTypeMapping myMapping =
      (new SoapReflectionImporter().ImportTypeMapping
      (typeof(Group)));
       return new XmlSerializer(myMapping);
   }
}

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1