Export (0) Print
Expand All

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 void Add (
	Type type,
	string member,
	XmlAttributes attributes
)
public void Add (
	Type type, 
	String member, 
	XmlAttributes attributes
)
public function Add (
	type : Type, 
	member : String, 
	attributes : XmlAttributes
)
Not applicable.

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.

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 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;
}
}

// This is the class that will be serialized.
public class Group
{
    public String groupName;
    /** @attribute XmlAttribute()
     */
    public int groupCode;
} //Group

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.set_AttributeName("Code");

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

        /* Add the XmlAttributes object to the XmlAttributeOverrides
           and specify the type and member name to override.
         */
        xOver.Add(Group.class.ToType(), "groupCode", attrs);
        XmlSerializer xSer = new XmlSerializer(Group.class.ToType(), xOver);
        return xSer;
    } //CreateOverrider
} //Sample

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft