共用方式為


XmlChoiceIdentifierAttribute Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Specifies that the member can be further detected by using an enumeration.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.Xml.Serialization.XmlChoiceIdentifierAttribute

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple := False)> _
Public Class XmlChoiceIdentifierAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple = false)]
public class XmlChoiceIdentifierAttribute : Attribute

The XmlChoiceIdentifierAttribute type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XmlChoiceIdentifierAttribute() Initializes a new instance of the XmlChoiceIdentifierAttribute class.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XmlChoiceIdentifierAttribute(String) Initializes a new instance of the XmlChoiceIdentifierAttribute class that specifies the name of the member that returns the enumeration used to detect a choice.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberName Gets or sets the name of the field that returns the enumeration to use when detecting types.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The XML schema element definition named xsi:choice is used to define a complex element that can contain only one child in an instance (maxoccurs = 1). That child can be one of several types and it can have one of several names. Each name is associated with a specific type; however, several names can be associated with the same type. Because of this, an instance of such an element is indistinct. For example, consider the following schema fragment that defines such an indistinct element named MyChoice.

 <xsd:complexType name="MyChoice">
  <xsd:sequence>
  <xsd:choice minOccurs="0" maxOccurs="1">
  <xsd:element minOccurs="1" maxOccurs="1" name="ChoiceOne" type="xsd:string" />
  <xsd:element minOccurs="1" maxOccurs="1" name="ChoiceTwo" type="xsd:string" />
  </xsd:choice>
  </xsd:sequence>
 </xsd:complexType>

The XmlChoiceIdentifierAttribute allows you to assign a special enumeration value to each instance of the member. The following C# code shows how the XmlChoiceIdentifierAttribute is applied to an Item field; the MemberName property identifies the field that contains the enumeration that is further used to detect the choice.

 public class Choices{
  [XmlChoiceIdentifier("ItemType")]
  [XmlChoiceIdentifier("ChoiceOne")]
  [XmlChoiceIdentifier("ChoiceTwo")]
  public string MyChoice;

  // Do not serialize this next field:
  [XmlIgnore]
  public ItemChoiceType ItemType;
 }
 // Do not include this enumeration in the XML schema.
 [XmlType(IncludeInSchema = false)]
 public enum ItemChoiceType{
  ChoiceOne,
  ChoiceTwo,
 }

When this code is in place, you can serialize and deserialize this class by setting the ItemType field to an appropriate enumeration. For example, to serialize the Choice class, the C# code resembles the following.

 Choices mc = new Choices();
 mc.MyChoice = "Item Choice One";
 mc.ItemType = ItemChoiceType.ChoiceOne;

When deserializing, the C# code resembles the following.

 MyChoice mc = (MyChoice) myXmlSerializer.Deserialize(myReader);
 if(mc.ItemType == ItemChoiceType.ChoiceOne)
  {
      // Handle choice one.
  }
 if(mc.ItemType == ItemChoiceType.ChoiceTwo)
  {
      // Handle choice two.
  }
 if(mc.ItemType != null)
  {
      throw CreateUnknownTypeException(mc.Item);
  }

There is a second scenario when the XmlChoiceIdentifierAttribute is used. In the following schema, the member is a field that returns an array of items (maxOccurs="unbounded"). The array can contain objects of the first choice ("D-a-t-a"), and of the second choice ("MoreData").

 <xsd:complexType name="MyChoice">
  <xsd:sequence>
  <xsd:choice minOccurs="0" maxOccurs="unbounded">
  <xsd:element minOccurs="1" maxOccurs="1" name="D-a-t-a" type="xsd:string" />
  <xsd:element minOccurs="1" maxOccurs="1" name="MoreData" type="xsd:string" />
  </xsd:choice>
  </xsd:sequence>
 </xsd:complexType>

The resulting class then uses a field to return an array of items. For each item in the array, a corresponding ItemChoiceType enumeration must also be found. The matching enumerations are contained in the array returned by the ItemsElementName field.

 public class MyChoice {
  [System.Xml.Serialization.XmlElementAttribute("D-a-t-a", typeof(string), IsNullable=false)]
  [System.Xml.Serialization.XmlElementAttribute("MoreData", typeof(string), IsNullable=false)]
  [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
  public string[] Items;
  [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)]
  [System.Xml.Serialization.XmlIgnoreAttribute()]
  public ItemsChoiceType[] ItemsElementName;
 }
 [System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]
 public enum ItemsChoiceType {
  [System.Xml.Serialization.XmlEnumAttribute("D-a-t-a")]
  Data,
  MoreData,
 }

When deserializing an object that includes a range of choices, use a control structure (such as an if...then...else structure) to determine how to deserialize a particular value. In the control structure, check the enumeration value and deserialize the value accordingly.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.