VB.Net Sample : Simple Choice
In This Topic
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 the element with ChangeAccountPassword element defined.
|
|
Sample XML File
Sample1.xml |
|
<?xml version="1.0" encoding="UTF-8"?>
<AccountAdminRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="..\Schema\SimpleChoice.xsd">
<CreateAccount Username="Guest" Password="Password"/>
</AccountAdminRequest>
|
Sample Code
Read Sample |
|
Public Shared Sub SampleRead(ByVal FilePath As String)
' Create the request object
Dim req As SimpleChoiceLib.AccountAdminRequest = New SimpleChoiceLib.AccountAdminRequest()
' Load data into the object from a file
req.FromXmlFile(FilePath)
' Because req is a choice only one of the elments in it may be valid
If (req.ChoiceSelectedElement = "ChangeAccountPassword") Then
' We will now delete the account
Console.WriteLine("Change the password for the account {0}, from {1} to {2}", req.ChangeAccountPassword.Username, req.ChangeAccountPassword.OldPassword, req.ChangeAccountPassword.NewPassword)
' ...
ElseIf (req.ChoiceSelectedElement = "CreateAccount") Then
' We will now create a new account...
Console.WriteLine("Create New account {0} with a password {1}", req.CreateAccount.Username, req.CreateAccount.Password)
' ...
ElseIf (req.ChoiceSelectedElement = "DeleteAccount") Then
' We will now delete the account
Console.WriteLine("Delete the account {0}", req.DeleteAccount.Username)
' ...
Else
' An unknown element was selected...This should be coded for as
' additional options may one day be added to the schema.
Console.WriteLine("Unknown selected element {0}", req.ChoiceSelectedElement)
End If
End Sub
...
SampleRead(Module1.SamplePath + "SimpleChoice\Samples\Sample1.xml")
Output
Create New account Guest with a password Password
|
|
Write Sample |
|
' Create DVD object
Dim req As SimpleChoiceLib.AccountAdminRequest = New SimpleChoiceLib.AccountAdminRequest()
' Now we can form a create account request
Dim naReq As SimpleChoiceLib.CreateAccount = New SimpleChoiceLib.CreateAccount()
naReq.Username = "Guest"
naReq.Password = "Password"
req.CreateAccount = naReq
' Now we can look at the XML from this object
Console.WriteLine(req.ToXml(True, System.Xml.Formatting.Indented, LiquidTechnologies.Runtime.EOLType.CRLF))
Output
<?xml version="1.0"?>
<!--Created by Liquid XML Data Binding Libraries (www.liquid-technologies.com) for Liquid Technologies Ltd -->
<AccountAdminRequest xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<CreateAccount Username="Guest" Password="Password"/>
</AccountAdminRequest>
|
|
XSD Source Files
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 |
|
|
Generated Files
AccountAdminRequest.vb |
|
Option Explicit On
Option Strict On
Imports System
Imports System.Xml
'**********************************************************************************************
'* 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
'**********************************************************************************************
Namespace SimpleChoiceLib
''' <summary>
''' This class represents the Element AccountAdminRequest
''' </summary>
<LiquidTechnologies.Runtime.XmlObjectInfo(LiquidTechnologies.Runtime.XmlObjectBase.XmlElementGroupType.Choice, _
LiquidTechnologies.Runtime.XmlObjectBase.XmlElementType.Element, _
"AccountAdminRequest", "", true, false, false)> _
Public Partial Class AccountAdminRequest
Inherits SimpleChoiceLib.XmlCommonBase
#Region "Constructors"
''' <summary>
''' Constructor for AccountAdminRequest
''' </summary>
''' <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
''' </remarks>
Public Sub New()
_elementName = "AccountAdminRequest"
Init()
End Sub
Public Sub New(ByVal elementName As String)
_elementName = elementName
Init()
End Sub
#End Region
#Region "Initialization methods for the class"
''' <summary>
''' Initializes the class
''' </summary>
''' <remarks>
''' This 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.
''' </remarks>
Protected Overrides Sub Init()
SimpleChoiceLib.Registration.iRegistrationIndicator = 0 ' causes registration to take place
_CreateAccount = Nothing
_DeleteAccount = Nothing
_ChangeAccountPassword = Nothing
_validElement = ""
' ##HAND_CODED_BLOCK_START ID="Additional Inits"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS
' Add Additional initialization code here...
' ##HAND_CODED_BLOCK_END ID="Additional Inits"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS
End Sub
Protected Sub ClearChoice(ByVal selectedElement As String)
_CreateAccount = Nothing
_DeleteAccount = Nothing
_ChangeAccountPassword = Nothing
_validElement = selectedElement
End Sub
#End Region
#Region "ICloneable Interface"
''' <summary>
''' Allows the class to be copied
''' </summary>
''' <remarks>
''' Performs a 'deep copy' of all the data in the class (and its children)
''' </remarks>
Public Overrides Function Clone() As Object
Dim newObject As New SimpleChoiceLib.AccountAdminRequest(_elementName)
Dim o As Object
newObject._CreateAccount = Nothing
if Not _CreateAccount Is Nothing Then
newObject._CreateAccount = CType(_CreateAccount.Clone(), SimpleChoiceLib.CreateAccount)
End If
newObject._DeleteAccount = Nothing
if Not _DeleteAccount Is Nothing Then
newObject._DeleteAccount = CType(_DeleteAccount.Clone(), SimpleChoiceLib.DeleteAccount)
End If
newObject._ChangeAccountPassword = Nothing
if Not _ChangeAccountPassword Is Nothing Then
newObject._ChangeAccountPassword = CType(_ChangeAccountPassword.Clone(), SimpleChoiceLib.ChangeAccountPassword)
End If
o = Nothing
newObject._validElement = _validElement
' ##HAND_CODED_BLOCK_START ID="Additional clone"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS
' Add Additional clone code here...
' ##HAND_CODED_BLOCK_END ID="Additional clone"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS
Return newObject
End Function
#End Region
#Region "Member variables"
Protected Overrides Readonly Property TargetNamespace() As String
Get
Return ""
End Get
End Property
#Region "Attribute - CreateAccount"
''' <summary>
''' Represents an optional Element in the XML document
''' </summary>
''' <remarks>
''' This property is represented as an Element in the XML.
''' It is optional, initially it is Nothing.
''' 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 Nothing will allow another element to be selected
''' </remarks>
<LiquidTechnologies.Runtime.ElementInfoChoiceClsOpt("CreateAccount", "", LiquidTechnologies.Runtime.XmlObjectBase.XmlElementType.Element, GetType(SimpleChoiceLib.CreateAccount))> _
Public Property CreateAccount() As SimpleChoiceLib.CreateAccount
Get
Return _CreateAccount
End Get
Set(ByVal value As SimpleChoiceLib.CreateAccount)
' The class represents a choice, so prevent more than one element from being selected
If value Is Nothing Then ClearChoice("") Else ClearChoice("CreateAccount") ' remove selection
If value Is Nothing Then
_CreateAccount = Nothing
Else
SetElementName(value, "CreateAccount")
_CreateAccount = value
End If
End Set
End Property
Protected _CreateAccount As SimpleChoiceLib.CreateAccount
#End Region
#Region "Attribute - DeleteAccount"
''' <summary>
''' Represents an optional Element in the XML document
''' </summary>
''' <remarks>
''' This property is represented as an Element in the XML.
''' It is optional, initially it is Nothing.
''' 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 Nothing will allow another element to be selected
''' </remarks>
<LiquidTechnologies.Runtime.ElementInfoChoiceClsOpt("DeleteAccount", "", LiquidTechnologies.Runtime.XmlObjectBase.XmlElementType.Element, GetType(SimpleChoiceLib.DeleteAccount))> _
Public Property DeleteAccount() As SimpleChoiceLib.DeleteAccount
Get
Return _DeleteAccount
End Get
Set(ByVal value As SimpleChoiceLib.DeleteAccount)
' The class represents a choice, so prevent more than one element from being selected
If value Is Nothing Then ClearChoice("") Else ClearChoice("DeleteAccount") ' remove selection
If value Is Nothing Then
_DeleteAccount = Nothing
Else
SetElementName(value, "DeleteAccount")
_DeleteAccount = value
End If
End Set
End Property
Protected _DeleteAccount As SimpleChoiceLib.DeleteAccount
#End Region
#Region "Attribute - ChangeAccountPassword"
''' <summary>
''' Represents an optional Element in the XML document
''' </summary>
''' <remarks>
''' This property is represented as an Element in the XML.
''' It is optional, initially it is Nothing.
''' 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 Nothing will allow another element to be selected
''' </remarks>
<LiquidTechnologies.Runtime.ElementInfoChoiceClsOpt("ChangeAccountPassword", "", LiquidTechnologies.Runtime.XmlObjectBase.XmlElementType.Element, GetType(SimpleChoiceLib.ChangeAccountPassword))> _
Public Property ChangeAccountPassword() As SimpleChoiceLib.ChangeAccountPassword
Get
Return _ChangeAccountPassword
End Get
Set(ByVal value As SimpleChoiceLib.ChangeAccountPassword)
' The class represents a choice, so prevent more than one element from being selected
If value Is Nothing Then ClearChoice("") Else ClearChoice("ChangeAccountPassword") ' remove selection
If value Is Nothing Then
_ChangeAccountPassword = Nothing
Else
SetElementName(value, "ChangeAccountPassword")
_ChangeAccountPassword = value
End If
End Set
End Property
Protected _ChangeAccountPassword As SimpleChoiceLib.ChangeAccountPassword
#End Region
Public Readonly Property ChoiceSelectedElement() As String
Get
Return _validElement
End Get
End Property
Protected _validElement As String
#Region "Attribute - Namespace"
Public Overrides Readonly Property [Namespace]() As String
Get
Return ""
End Get
End Property
#End Region
#Region "Attribute - GetBase"
Public Overrides Function GetBase() As LiquidTechnologies.Runtime.XmlObjectBase
Return Me
End Function
#End Region
#End Region
' ##HAND_CODED_BLOCK_START ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS
' Add Additional Methods and members here...
' ##HAND_CODED_BLOCK_END ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS
End Class
End Namespace
|