XmlAttributeOverrides.Add Method (Type, String, XmlAttributes)

 

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 is overridden.

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

Public Sub Add (
	type As Type,
	member As String,
	attributes As XmlAttributes
)

Parameters

type
Type: System.Type

The Type of the object to override.

member
Type: System.String

The name of the member to override.

attributes
Type: System.Xml.Serialization.XmlAttributes

An XmlAttributes object that represents the overriding attributes.

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 XML attribute 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.

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.

' 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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: