This documentation is archived and is not being maintained.

XmlAttributeOverrides.Add Method (Type, String, XmlAttributes)

.NET Framework 1.1

Adds an XmlAttributes object to the collection of XmlAttributes objects. The type parameter specifies an object to be overridden. The member parameter specifies the name of a member that will be overridden.

[Visual Basic]
Overloads Public Sub Add( _
   ByVal type As Type, _
   ByVal member As String, _
   ByVal attributes As XmlAttributes _
)
[C#]
public void Add(
 Type type,
 string member,
 XmlAttributes attributes
);
[C++]
public: void Add(
 Type* type,
 String* member,
 XmlAttributes* attributes
);
[JScript]
public function Add(
   type : Type,
 member : String,
 attributes : XmlAttributes
);

Parameters

type
The Type of the object to override.
member
The name of the member to override.
attributes
An XmlAttributes object that represents the overriding attributes.

Remarks

The XmlAttributes object contains a union of attribute objects that cause the XmlSerializer to override its default serialization behavior for a set of objects. You choose the attribute objects to place in the XmlAttributes object, depending on the particular behaviors you want to override. For example, the XmlSerializer serializes a class member as an XML element by default. If you want the member to be serialized as an XmlAttribute instead, you would create an XmlAttributeAttribute, assign it to the XmlAttribute property of an XmlAttributes, and add the XmlAttributes object to the XmlAttributeOverrides object.

Use this method when attempting to override an XmlElementAttribute, XmlAttributeAttribute, XmlArrayAttribute, XmlArrayItemAttribute, or XmlIgnoreAttribute.

Example

[Visual Basic, C#, C++] The following example creates an XmlAttributeAttribute object, and assigns it to the XmlAttribute property of an XmlAttributes object. The example then adds the XmlAttributes object to an XmlAttributeOverrides object, before creating an XmlSerializer.

[Visual Basic] 
' This is the class that will be serialized.
Public Class Group
    Public GroupName As String
    <XmlAttribute()> Public GroupCode As Integer
End Class

Public Class Sample
    
    Public Function CreateOverrider() As XmlSerializer
        ' Create an XmlAttributeOverrides object. 
        Dim xOver As New XmlAttributeOverrides()
        
        ' Create an XmlAttributeAttribute to override the base class
        ' object's XmlAttributeAttribute object. Give the overriding object
        ' a new attribute name ("Code").
        Dim xAtt As New XmlAttributeAttribute()
        xAtt.AttributeName = "Code"
        
        ' Create an instance of the XmlAttributes class and set the
        ' XmlAttribute property to the XmlAttributeAttribute object. 
        Dim attrs As New XmlAttributes()
        attrs.XmlAttribute = xAtt
        
        ' Add the XmlAttributes object to the XmlAttributeOverrides
        ' and specify the type and member name to override. 
        xOver.Add(GetType(Group), "GroupCode", attrs)
        
        Dim xSer As New XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function
End Class


[C#] 
// This is the class that will be serialized.
public class Group
{
   public string GroupName;
   [XmlAttribute]
   public int GroupCode;
}

public class Sample
{
public XmlSerializer CreateOverrider()
{
   // Create an XmlAttributeOverrides object. 
   XmlAttributeOverrides xOver = new XmlAttributeOverrides();

   /* Create an XmlAttributeAttribute to override the base class
   object's XmlAttributeAttribute object. Give the overriding object
   a new attribute name ("Code"). */
   XmlAttributeAttribute xAtt = new XmlAttributeAttribute();
   xAtt.AttributeName = "Code";

   /* Create an instance of the XmlAttributes class and set the 
   XmlAttribute property to the XmlAttributeAttribute object. */
   XmlAttributes attrs = new XmlAttributes();
   attrs.XmlAttribute = xAtt;

   /* Add the XmlAttributes object to the XmlAttributeOverrides
      and specify the type and member name to override. */
   xOver.Add(typeof(Group), "GroupCode", attrs);

   XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
   return xSer;
}
}

[C++] 
// This is the class that will be serialized.
public __gc class Group
{
public:
   String* GroupName;
   [XmlAttributeAttribute]
   int GroupCode;
};

public __gc class Sample
{
public:
   XmlSerializer* CreateOverrider()
   {
      // Create an XmlAttributeOverrides object. 
      XmlAttributeOverrides* xOver = new XmlAttributeOverrides();

      /* Create an XmlAttributeAttribute to override the base class
      object's XmlAttributeAttribute object. Give the overriding object
      a new attribute name ("Code"). */
      XmlAttributeAttribute* xAtt = new XmlAttributeAttribute();
      xAtt->AttributeName = S"Code";

      /* Create an instance of the XmlAttributes class and set the 
      XmlAttribute property to the XmlAttributeAttribute object. */
      XmlAttributes* attrs = new XmlAttributes();
      attrs->XmlAttribute = xAtt;

      /* Add the XmlAttributes object to the XmlAttributeOverrides
      and specify the type and member name to override. */
      xOver->Add(__typeof(Group), S"GroupCode", attrs);

      XmlSerializer* xSer = new XmlSerializer(__typeof(Group), xOver);
      return xSer;
   }
};

[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

XmlAttributeOverrides Class | XmlAttributeOverrides Members | System.Xml.Serialization Namespace | XmlAttributeOverrides.Add Overload List | XmlAttributes | XmlSerializer

Show: