Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

170 lines
9.4 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<Type Name="XmlChoiceIdentifierAttribute" FullName="System.Xml.Serialization.XmlChoiceIdentifierAttribute">
<TypeSignature Maintainer="auto" Language="C#" Value="public class XmlChoiceIdentifierAttribute : Attribute" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit XmlChoiceIdentifierAttribute extends System.Attribute" />
<AssemblyInfo>
<AssemblyName>System.Xml</AssemblyName>
<AssemblyPublicKey>[00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00]</AssemblyPublicKey>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ThreadSafetyStatement>To be added</ThreadSafetyStatement>
<Base>
<BaseTypeName>System.Attribute</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute>
<AttributeName>System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue | System.AttributeTargets.All)</AttributeName>
</Attribute>
</Attributes>
<Docs>
<remarks>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>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.</para>
<code> &lt;xsd:complexType name="MyChoice"&gt;
&lt;xsd:sequence&gt;
&lt;xsd:choice minOccurs="0" maxOccurs="1"&gt;
&lt;xsd:element minOccurs="1" maxOccurs="1" name="ChoiceOne" type="xsd:string" /&gt;
&lt;xsd:element minOccurs="1" maxOccurs="1" name="ChoiceTwo" type="xsd:string" /&gt;
&lt;/xsd:choice&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;</code>
<para>The <see cref="T:System.Xml.Serialization.XmlChoiceIdentifierAttribute" /> allows you to assign a special enumeration value to each instance of the member. You must either create the enumeration yourself or it can be generated by the <format type="text/html"><a href="a6e6e65c-347f-4494-9457-653bf29baac2">XML Schema Definition Tool (Xsd.exe)</a></format>. The following C# code shows how the <see cref="T:System.Xml.Serialization.XmlChoiceIdentifierAttribute" /> is applied to an Item field; the <see cref="P:System.Xml.Serialization.XmlChoiceIdentifierAttribute.MemberName" /> property identifies the field that contains the enumeration that is further used to detect the choice.</para>
<code> 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,
}</code>
<para>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.</para>
<code> Choices mc = new Choices();
mc.MyChoice = "Item Choice One";
mc.ItemType = ItemChoiceType.ChoiceOne;</code>
<para>When deserializing, the C# code resembles the following: </para>
<code> 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);
}</code>
<para>There is a second scenario when the <see cref="T:System.Xml.Serialization.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").</para>
<code> &lt;xsd:complexType name="MyChoice"&gt;
&lt;xsd:sequence&gt;
&lt;xsd:choice minOccurs="0" maxOccurs="unbounded"&gt;
&lt;xsd:element minOccurs="1" maxOccurs="1" name="D-a-t-a" type="xsd:string" /&gt;
&lt;xsd:element minOccurs="1" maxOccurs="1" name="MoreData" type="xsd:string" /&gt;
&lt;/xsd:choice&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;</code>
<para>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.</para>
<code> 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,
}</code>
<para>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.</para>
</remarks>
<summary>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>Specifies that the member can be further detected by using an enumeration.</para>
</summary>
</Docs>
<Members>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public XmlChoiceIdentifierAttribute ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue />
<Parameters />
<Docs>
<remarks>To be added</remarks>
<summary>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>Initializes a new instance of the <see cref="T:System.Xml.Serialization.XmlChoiceIdentifierAttribute" /> class.</para>
</summary>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public XmlChoiceIdentifierAttribute (string name);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string name) cil managed" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue />
<Parameters>
<Parameter Name="name" Type="System.String" />
</Parameters>
<Docs>
<remarks>To be added</remarks>
<summary>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>Initializes a new instance of the <see cref="T:System.Xml.Serialization.XmlChoiceIdentifierAttribute" /> class.</para>
</summary>
<param name="name">
<attribution license="cc4" from="Microsoft" modified="false" />The member name that returns the enumeration used to detect a choice. </param>
</Docs>
</Member>
<Member MemberName="MemberName">
<MemberSignature Language="C#" Value="public string MemberName { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance string MemberName" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.String</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<value>To be added: an object of type 'string'</value>
<remarks>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>At least one member must be present in the enumeration returned by the field named in the <see cref="P:System.Xml.Serialization.XmlChoiceIdentifierAttribute.MemberName" /> value. By default, that enumeration name takes the name of the field that the <see cref="T:System.Xml.Serialization.XmlChoiceIdentifierAttribute" /> is applied to.</para>
</remarks>
<summary>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>Gets or sets the name of the field that returns the enumeration to use when detecting types.</para>
</summary>
</Docs>
</Member>
</Members>
</Type>