Share via


XML Schema Choice Element Proxy Artifacts

Topic Last Modified: 2007-04-13

The object model that Web service proxy generators create can establish complex object relationships. When the proxy generator handles XML schema choice elements, a complex object relationship is created.

The Microsoft Visual Studio 2005 proxy generator handles the XML schema choice element by returning either a shared base class for the choices, or, if the choices do not share a common base class, an object. In either case, the object that is returned must be cast to the appropriate type so that the client application can access all the properties on the object.

Remarks

You can find all the choice elements in the schema by searching for <xs:choice> in the types.xsd and messages.xsd files.

Examples

Description

The XML example shows the FindItemParentType type that contains an XML schema choice element from the types.xsd schema file. Note that ArrayOfRealItemsType and ArrayOfGroupedItemsType do not share a common base type.

The C# example uses the FindItemParentType proxy class that is generated by Visual Studio 2005. Notice that the Item property of FindItemParentType returns an object of type Object.

Code

<xs:complexType name="FindItemParentType">
  <xs:choice>
    <xs:element name="Items" type="t:ArrayOfRealItemsType"/>
    <xs:element name="Groups" type="t:ArrayOfGroupedIetmsType"/>
  </xs:choice>
  <xs:attributeGroup ref="t:FindResponsePagingAttributes"/>
</xs:complexType>

Code

FindItemParentType fipt = new FindItemParentType();
object obj = fipt.Item;
if (obj is ArrayOfGroupedItemsType)
{
    ArrayOfGroupedItemsType groupedItems = (obj as ArrayOfGroupedItemsType);
    //TODO: Write code to handle grouped items.
}
else if (obj is ArrayOfRealItemsType)
{
    ArrayOfRealItemsType items = (obj as ArrayOfRealItemsType);
    //TODO: Write code to handle items.
}

Description

The XML example shows a type that contains an XML schema choice element from the types.xsd schema file. The TargetFolderIdType schema type is different from the FindItemParentType schema type in the previous example in that the choice options both share a common base type named BaseFolderIdType. The FindItemParentType choice options did not share a common base type other than type Object.

The C# example shows how the Item property of the TargetFolderIdType returns an object of type BaseFolderIdType instead of type Object.

Code

<xs:complexType name="TargetFolderIdType">
  <xs:choice>
    <xs:element name="FolderId" type="t:FolderIdType"/>
    <xs:element name="DistinguishedFolderId" type="t:DistinguishedFolderIdType"/>
  </xs:choice>
</xs:complexType>

Code

TargetFolderIdType tfit = new TargetFolderIdType();
BaseFolderIdType bfit = tfit.Item;
if (bfit is FolderIdType)
{
    FolderIdType folder = (bfit as FolderIdType);
    // TODO: Write code to access the folder properties.
}
else if (bfit is DistinguishedFolderIdType)
{
    DistinguishedFolderIdType distFolder = (bfit as DistinguishedFOlderIdType);
    // TODO: Write code to access the folder properties.
}

Comments

If the client application only needs to access properties that are contained in the base type, the cast is an unnecessary operation.