XmlAttributeOverrides.Item[] Property

Definition

Gets an object that represents the collection of overriding attributes.

Overloads

Item[Type]

Gets the object associated with the specified, base-class, type.

Item[Type, String]

Gets the object associated with the specified (base-class) type. The member parameter specifies the base-class member that is overridden.

Item[Type]

Source:
XmlAttributeOverrides.cs
Source:
XmlAttributeOverrides.cs
Source:
XmlAttributeOverrides.cs

Gets the object associated with the specified, base-class, type.

C#
public System.Xml.Serialization.XmlAttributes this[Type type] { get; }
C#
public System.Xml.Serialization.XmlAttributes? this[Type type] { get; }

Parameters

type
Type

The base class Type that is associated with the collection of attributes you want to retrieve.

Property Value

An XmlAttributes that represents the collection of overriding attributes.

Examples

The following example creates an XmlAttributeOverrides object, an XmlAttributes object, and an XmlRootAttribute object. The example assigns the XmlRootAttribute to the XmlRoot property of the XmlAttributes object, and adds the XmlAttributes object to the XmlAttributeOverrides object. Lastly, the example gets the XmlAttributes object by passing the Type of the serialized class to the XmlAttributeOverrides object. In this example, the Type is Group.

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 XmlSerializer with overriding attributes.
   XmlAttributes attrs = new XmlAttributes();
   XmlAttributeOverrides xOver = new XmlAttributeOverrides();

   XmlRootAttribute xRoot = new XmlRootAttribute();
   // Set a new Namespace and ElementName for the root element.
   xRoot.Namespace = "http://www.cpandl.com";
   xRoot.ElementName = "NewGroup";
   attrs.XmlRoot = xRoot;

   xOver.Add(typeof(Group), attrs);

   // Get the XmlAttributes object, based on the type.
   XmlAttributes tempAttrs;
   tempAttrs = xOver[typeof(Group)];

   // Print the Namespace and ElementName of the root.
   Console.WriteLine(tempAttrs.XmlRoot.Namespace);
   Console.WriteLine(tempAttrs.XmlRoot.ElementName);

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

Remarks

Use this overload to return an XmlAttributes object that contains attributes for an XmlRootAttribute or XmlTypeAttribute object.

If the XmlAttributes object contains objects that override an XmlArrayAttribute, XmlArrayItemAttribute, XmlElementAttribute, XmlEnumAttribute, or XmlAttributeAttribute, you must use the overload that specifies the overridden member as well as the type.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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
UWP 10.0

Item[Type, String]

Source:
XmlAttributeOverrides.cs
Source:
XmlAttributeOverrides.cs
Source:
XmlAttributeOverrides.cs

Gets the object associated with the specified (base-class) type. The member parameter specifies the base-class member that is overridden.

C#
public System.Xml.Serialization.XmlAttributes this[Type type, string member] { get; }
C#
public System.Xml.Serialization.XmlAttributes? this[Type type, string member] { get; }

Parameters

type
Type

The base class Type that is associated with the collection of attributes you want.

member
String

The name of the overridden member that specifies the XmlAttributes to return.

Property Value

An XmlAttributes that represents the collection of overriding attributes.

Examples

The following example creates an XmlAttributeOverrides object, an XmlAttributes, and an XmlAttributeAttribute object. The example assigns the XmlAttributeAttribute to the XmlAttribute property of the XmlAttributes object and adds the XmlAttributes object to the XmlAttributeOverrides object. Lastly, the example gets the XmlAttributes object by passing the Type of the serialized class and member name to the XmlAttributeOverrides object.

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 XmlSerializer with overriding attributes.
   XmlAttributeOverrides xOver = new XmlAttributeOverrides();

   /* Create an XmlAttributeAttribute object and set the
   AttributeName property. */
   XmlAttributeAttribute xAtt = new XmlAttributeAttribute();
   xAtt.AttributeName = "Code";

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

   /* Add the XmlAttributes to the XmlAttributeOverrides object. The
   name of the overridden attribute must be specified. */
   xOver.Add(typeof(Group), "GroupCode", attrs);

   // Get the XmlAttributes object for the type and member.
   XmlAttributes tempAttrs;
   tempAttrs = xOver[typeof(Group), "GroupCode"];
   Console.WriteLine(tempAttrs.XmlAttribute.AttributeName);

   // Create the XmlSerializer instance and return it.
   XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
   return xSer;
}
}

Remarks

Use this overload to return an XmlAttributes object that contains objects that override an XmlArrayAttribute, XmlArrayItemAttribute, XmlAttributeAttribute, XmlElementAttribute, or XmlEnumAttribute. If the XmlAttributes object contains an XmlRootAttribute or XmlTypeAttribute, you must use the overload that specifies only the overridden type.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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
UWP 10.0