Visual Basic 6 Sample : Simple Choice
Schema Summary This sample shows an element containing a choice of simple elements. One and only one child element may be provided. Schema Details The element AccountAdminRequest contains 3 child elements, CreateAccount, DeleteAccount and ChangeAccountPassword. Only one of these elements may be specified at a time, and one MUST be specified. All 3 of the child elements are represented in the generated code as classes (as opposed to primitive), despite being based on primitive types. This is because they all contain child attributes of there own. Elements with child elements or attributes must be represented as classes in the generated code. Sample Description The sample demonstrates what happens when more than one item in a choice is set. |
Sample Code
Write Sample | |
' Create objects Dim req As New SimpleChoiceLib.AccountAdminRequest Dim naReq As New SimpleChoiceLib.CreateAccount Dim daReq As New SimpleChoiceLib.DeleteAccount On Error GoTo Error ' Now we can form a create account request naReq.Username = "Guest" naReq.Password = "Password" Set req.CreateAccount = naReq ' Now we can look at the XML from this object Trace req.ToXml(True, XmlFormatting_Indented, XmlEncoding_UTF8) daReq.Username = "Guest" Set req.DeleteAccount = daReq Trace req.ToXml(True, XmlFormatting_Indented, XmlEncoding_UTF8) Exit Sub Error: Trace "Error: " &; Err.Description
|
SimpleChoice.xsd |
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="AccountAdminRequest"> <xs:complexType> <xs:choice> <xs:element name="CreateAccount"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="Username" type="xs:string" use="required"/> <xs:attribute name="Password" type="xs:string" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="DeleteAccount"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="Username" type="xs:string" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="ChangeAccountPassword"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:date"> <xs:attribute name="Username" type="xs:string" use="required"/> <xs:attribute name="OldPassword" type="xs:string" use="required"/> <xs:attribute name="NewPassword" type="xs:string" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> |
Schema Diagrams |
|
AccountAdminRequest.cls |
VERSION 1.0 CLASS BEGIN MultiUse = -1 'True Persistable = 0 'NotPersistable DataBindingBehavior = 0 'vbNone DataSourceBehavior = 0 'vbNone MTSTransactionMode = 0 'NotAnMTSObject END Attribute VB_Name = "AccountAdminRequest" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = True Option Explicit '********************************************************************************************** '* Copyright (c) 2001-2023 Liquid Technologies Limited. All rights reserved. '* See www.liquid-technologies.com for product details. '* '* Please see products End User License Agreement for distribution permissions. '* '* WARNING: THIS FILE IS GENERATED '* Changes made outside of ##HAND_CODED_BLOCK_START blocks will be overwritten '* '* Generation : by Liquid XML Data Binder 19.0.14.11049 '* Using Schema: SimpleChoice.xsd '********************************************************************************************** Private m_ElementName As String Private m_validElement As String Private mvarCreateAccount As SimpleChoiceLib.CreateAccount Private mvarDeleteAccount As SimpleChoiceLib.DeleteAccount Private mvarChangeAccountPassword As SimpleChoiceLib.ChangeAccountPassword ' ##HAND_CODED_BLOCK_START ID="Additional Variable Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS ' Add Additional Variable Declarations... ' ##HAND_CODED_BLOCK_END ID="Additional Variable Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS Implements LtXmlComLib20.XmlObjectBase Implements LtXmlComLib20.XmlGeneratedClass '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''' Properties ''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Property Get ElementName() As String ElementName = m_elementName End Property ' Summary: ' Represents an optional Element in the XML document ' ' Remarks: ' ' This property is represented as an Element in the XML. ' It is optional, initially it is null. ' Only one Element within this class may be set at a time, setting this property when another element is already set will raise an exception. setting this property to null will allow another element to be selected Public Property Get CreateAccount As SimpleChoiceLib.CreateAccount Set CreateAccount = mvarCreateAccount End Property Public Property Set CreateAccount(ByVal value As SimpleChoiceLib.CreateAccount) ' Ensure only on element is populated at a time ClearChoice "CreateAccount" ' remove selection if value is Nothing then Set mvarCreateAccount = Nothing else CastToXmlObjectBase(value).PrivateSetElementName "CreateAccount" ' LtXmlComLib20.XmlObjectBaseHelper.Throw_IfElementNameDiffers value, "CreateAccount" Set mvarCreateAccount = value End If End Property ' Summary: ' Represents an optional Element in the XML document ' ' Remarks: ' ' This property is represented as an Element in the XML. ' It is optional, initially it is null. ' Only one Element within this class may be set at a time, setting this property when another element is already set will raise an exception. setting this property to null will allow another element to be selected Public Property Get DeleteAccount As SimpleChoiceLib.DeleteAccount Set DeleteAccount = mvarDeleteAccount End Property Public Property Set DeleteAccount(ByVal value As SimpleChoiceLib.DeleteAccount) ' Ensure only on element is populated at a time ClearChoice "DeleteAccount" ' remove selection if value is Nothing then Set mvarDeleteAccount = Nothing else CastToXmlObjectBase(value).PrivateSetElementName "DeleteAccount" ' LtXmlComLib20.XmlObjectBaseHelper.Throw_IfElementNameDiffers value, "DeleteAccount" Set mvarDeleteAccount = value End If End Property ' Summary: ' Represents an optional Element in the XML document ' ' Remarks: ' ' This property is represented as an Element in the XML. ' It is optional, initially it is null. ' Only one Element within this class may be set at a time, setting this property when another element is already set will raise an exception. setting this property to null will allow another element to be selected Public Property Get ChangeAccountPassword As SimpleChoiceLib.ChangeAccountPassword Set ChangeAccountPassword = mvarChangeAccountPassword End Property Public Property Set ChangeAccountPassword(ByVal value As SimpleChoiceLib.ChangeAccountPassword) ' Ensure only on element is populated at a time ClearChoice "ChangeAccountPassword" ' remove selection if value is Nothing then Set mvarChangeAccountPassword = Nothing else CastToXmlObjectBase(value).PrivateSetElementName "ChangeAccountPassword" ' LtXmlComLib20.XmlObjectBaseHelper.Throw_IfElementNameDiffers value, "ChangeAccountPassword" Set mvarChangeAccountPassword = value End If End Property Public Property Get ChoiceSelectedElement As String ChoiceSelectedElement = m_validElement End Property friend Property Let ChoiceSelectedElement (ByVal value As String) m_validElement = value End Property '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''' Standard Methods ''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub ToXmlFile(ByVal FileName As String, Optional includeDocHeader As Boolean = True, Optional formatting As LtXmlComLib20.XmlFormatting = LtXmlComLib20.XmlFormatting_Indented, Optional encoding As LtXmlComLib20.XmlEncoding = LtXmlComLib20.XmlEncoding_UTF8, Optional EOL As LtXmlComLib20.EOLType = LtXmlComLib20.EOLType.EOLType_CRLF, Optional context As LtXmlComLib20.XmlSerializationContext = Nothing) RegisterProduct LtXmlComLib20.XmlObjectBaseHelper.ToXmlFile Me, FileName, includeDocHeader, formatting, encoding, EOL, context End Sub Public Function ToXml(Optional includeDocHeader As Boolean = True, Optional formatting As LtXmlComLib20.XmlFormatting = LtXmlComLib20.XmlFormatting_Indented, Optional EOL As LtXmlComLib20.EOLType = LtXmlComLib20.EOLType.EOLType_LF, Optional context As LtXmlComLib20.XmlSerializationContext = Nothing) As String RegisterProduct ToXml = LtXmlComLib20.XmlObjectBaseHelper.ToXml(Me, includeDocHeader, formatting, EOL, context) End Function Public Function ToXmlStream(Optional includeDocHeader As Boolean = True, Optional formatting As LtXmlComLib20.XmlFormatting = LtXmlComLib20.XmlFormatting_Indented, Optional encoding As LtXmlComLib20.XmlEncoding = LtXmlComLib20.XmlEncoding_UTF8, Optional EOL As LtXmlComLib20.EOLType = LtXmlComLib20.EOLType.EOLType_LF, Optional context As LtXmlComLib20.XmlSerializationContext = Nothing) As Variant RegisterProduct ToXmlStream = LtXmlComLib20.XmlObjectBaseHelper.ToXmlStream(Me, includeDocHeader, formatting, encoding, EOL, context) End Function Public Sub FromXml(ByVal xmlIn As String, Optional context As LtXmlComLib20.XmlSerializationContext = Nothing) RegisterProduct LtXmlComLib20.XmlObjectBaseHelper.FromXml Me, xmlIn, context End Sub Public Sub FromXmlFile(ByVal FileName As String, Optional context As LtXmlComLib20.XmlSerializationContext = Nothing) RegisterProduct LtXmlComLib20.XmlObjectBaseHelper.FromXmlFile Me, FileName, context End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''' Private Methods '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Summary: ' Constructor for AccountAdminRequest ' Remarks: ' The class is created with all the mandatory fields populated with the ' default data. ' All Collection object are created. ' However any 1-n relationships (these are represented as collections) are ' empty. To comply with the schema these must be populated before the xml ' obtained from ToXml is valid against the schema SimpleChoice.xsd Private Sub Class_Initialize() m_elementName = "AccountAdminRequest" XmlGeneratedClass_Init End Sub ' Summary: ' Selects the given element as the selected choice Private Sub ClearChoice(ByVal SelectedElement as String) Set mvarCreateAccount = Nothing Set mvarDeleteAccount = Nothing Set mvarChangeAccountPassword = Nothing m_validElement = selectedElement End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''' Implementation of XmlObjectBase ''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub XmlObjectBase_PrivateSetElementName(ByVal vNewValue As String) m_ElementName = vNewValue End Sub Private Property Get XmlObjectBase_ElementName() As String XmlObjectBase_ElementName = m_ElementName End Property Private Property Get XmlObjectBase_TargetNamespace() As String XmlObjectBase_TargetNamespace = "" End Property Private Function XmlObjectBase_IsSuitableSubstitution(ByVal InterfaceName As String) As Boolean XmlObjectBase_IsSuitableSubstitution = false if InterfaceName = "AccountAdminRequest" _ then XmlObjectBase_IsSuitableSubstitution = true End Function Private Property Get XmlObjectBase_Namespace() As String XmlObjectBase_Namespace = "" End Property Private Function XmlObjectBase_FromXmlInt(ByVal XMLParent As MSXML2.IXMLDOMElement, ByVal XMLChild As MSXML2.IXMLDOMElement, ByVal context As LtXmlComLib20.XmlSerializationContext, ByVal isOptionalChoice As Boolean) As MSXML2.IXMLDOMElement Set XmlObjectBase_FromXmlInt = XmlGeneratedClassHelper.FromXml(Me, XMLParent, XMLChild, context, isOptionalChoice) End Function Private Sub XmlObjectBase_ToXmlInt(ByVal xmlOut As LtXmlComLib20.XmlTextWriter, ByVal bRegisterNamespaces As Boolean, ByVal NamespaceUri As String, ByVal context As LtXmlComLib20.XmlSerializationContext, ByVal isOptionalChoice As Boolean) XmlGeneratedClassHelper.ToXml Me, xmlOut, bRegisterNamespaces, NamespaceUri, context, isOptionalChoice End Sub Private Sub XmlObjectBase_AttributesToXmlInt(ByVal xmlOut As LtXmlComLib20.XmlTextWriter, ByVal context As LtXmlComLib20.XmlSerializationContext) XmlGeneratedClassHelper.AttributesToXml Me, xmlOut, context End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''' Implementation of XmlGeneratedClass ''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Summary: ' Initializes the class ' Remarks: ' The Creates all the mandatory fields (populated with the default data) ' All Collection object are created. ' However any 1-n relationships (these are represented as collections) are ' empty. To comply with the schema these must be populated before the xml ' obtained from ToXml is valid against the schema SimpleChoice.xsd. Private Sub XmlGeneratedClass_Init() Set mvarCreateAccount = Nothing Set mvarDeleteAccount = Nothing Set mvarChangeAccountPassword = Nothing m_validElement = "" ' Force Init ClassInfo Dim classInfo As LtXmlComLib20.ClassInfo Set classInfo = XmlGeneratedClass_ClassInfo ' ##HAND_CODED_BLOCK_START ID="Additional Inits"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS ' Add Additional Init Settings... ' ##HAND_CODED_BLOCK_END ID="Additional Inits"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS End Sub Private Property Get XmlGeneratedClass_ClassInfo() As LtXmlComLib20.ClassInfo If g_ClsDataAccountAdminRequest Is Nothing Then Set g_ClsDataAccountAdminRequest = New LtXmlComLib20.ClassInfo g_ClsDataAccountAdminRequest.GroupType = LtXmlComLib20.XmlGroupType.Choice g_ClsDataAccountAdminRequest.ElementType = LtXmlComLib20.XmlElementType.Element g_ClsDataAccountAdminRequest.ElementName = "AccountAdminRequest" g_ClsDataAccountAdminRequest.ElementNamespaceURI = "" g_ClsDataAccountAdminRequest.FromXmlFailIfAttributeUnknown = true g_ClsDataAccountAdminRequest.IsClassDerived = false g_ClsDataAccountAdminRequest.PrimitiveDataType = LtXmlComLib20.XmlDataType.type_none g_ClsDataAccountAdminRequest.PrimitiveFormatOverride = "" g_ClsDataAccountAdminRequest.OutputPrimitiveClassAsTextProperty = False Set g_ClsDataAccountAdminRequest.ClassFactory = General.CF g_ClsDataAccountAdminRequest.AddElmChoiceClsOpt "CreateAccount", "", "CreateAccount", LtXmlComLib20.XmlElementType.Element, ClsName_CreateAccount g_ClsDataAccountAdminRequest.AddElmChoiceClsOpt "DeleteAccount", "", "DeleteAccount", LtXmlComLib20.XmlElementType.Element, ClsName_DeleteAccount g_ClsDataAccountAdminRequest.AddElmChoiceClsOpt "ChangeAccountPassword", "", "ChangeAccountPassword", LtXmlComLib20.XmlElementType.Element, ClsName_ChangeAccountPassword End If Set XmlGeneratedClass_ClassInfo = g_ClsDataAccountAdminRequest End Property ' ##HAND_CODED_BLOCK_START ID="Additional Methods/Properties"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS ' Add Additional Methods/Properties Here... ' ##HAND_CODED_BLOCK_END ID="Additional Methods/Properties"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS |
Main Menu | Samples List |