@@ -917,6 +917,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
917917 }
918918
919919 var effectiveElement = substitute ? . Element ?? element ;
920+ var isNullableByChoice = IsNullableByChoice ( item . XmlParent ) ;
920921 var propertyName = _configuration . NamingProvider . ElementNameFromQualifiedName ( effectiveElement . QualifiedName , effectiveElement ) ;
921922 var originalPropertyName = propertyName ;
922923 if ( propertyName == typeModel . Name )
@@ -934,9 +935,9 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
934935 OriginalPropertyName = originalPropertyName ,
935936 Type = substitute ? . Type ?? CreateTypeModel ( element . ElementSchemaType , elementQualifiedName ) ,
936937 IsNillable = element . IsNillable ,
937- IsNullable = item . MinOccurs < 1.0m || ( item . XmlParent is XmlSchemaChoice ) ,
938+ IsNullable = item . MinOccurs < 1.0m || isNullableByChoice ,
938939 IsCollection = item . MaxOccurs > 1.0m || particle . MaxOccurs > 1.0m , // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx
939- DefaultValue = element . DefaultValue ?? ( ( item . MinOccurs >= 1.0m && item . XmlParent is not XmlSchemaChoice ) ? element . FixedValue : null ) ,
940+ DefaultValue = element . DefaultValue ?? ( ( item . MinOccurs >= 1.0m && ! isNullableByChoice ) ? element . FixedValue : null ) ,
940941 FixedValue = element . FixedValue ,
941942 XmlNamespace = ! string . IsNullOrEmpty ( effectiveElement . QualifiedName . Namespace ) && effectiveElement . QualifiedName . Namespace != typeModel . XmlSchemaName . Namespace
942943 ? effectiveElement . QualifiedName . Namespace : null ,
@@ -969,7 +970,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
969970 OwningType = typeModel ,
970971 Name = "Any" ,
971972 Type = new SimpleModel ( _configuration ) { ValueType = ( _configuration . UseXElementForAny ? typeof ( XElement ) : typeof ( XmlElement ) ) , UseDataTypeAttribute = false } ,
972- IsNullable = item . MinOccurs < 1.0m || ( item . XmlParent is XmlSchemaChoice ) ,
973+ IsNullable = item . MinOccurs < 1.0m || IsNullableByChoice ( item . XmlParent ) ,
973974 IsCollection = item . MaxOccurs > 1.0m || particle . MaxOccurs > 1.0m , // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx
974975 IsAny = true ,
975976 XmlParticle = item . XmlParticle ,
@@ -1020,6 +1021,27 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
10201021 return properties ;
10211022 }
10221023
1024+ private static bool IsNullableByChoice ( XmlSchemaObject parent )
1025+ {
1026+ while ( parent != null )
1027+ {
1028+ switch ( parent )
1029+ {
1030+ case XmlSchemaChoice :
1031+ return true ;
1032+ // Any ancestor element between the current item and the
1033+ // choice would already have been forced to nullable.
1034+ case XmlSchemaElement :
1035+ case XmlSchemaParticle p when p . MinOccurs < 1.0m :
1036+ return false ;
1037+ default :
1038+ break ;
1039+ }
1040+ parent = parent . Parent ;
1041+ }
1042+ return false ;
1043+ }
1044+
10231045 private NamespaceModel CreateNamespaceModel ( Uri source , XmlQualifiedName qualifiedName )
10241046 {
10251047 NamespaceModel namespaceModel = null ;
0 commit comments