@@ -917,6 +917,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
917
917
}
918
918
919
919
var effectiveElement = substitute ? . Element ?? element ;
920
+ var isNullableByChoice = IsNullableByChoice ( item . XmlParent ) ;
920
921
var propertyName = _configuration . NamingProvider . ElementNameFromQualifiedName ( effectiveElement . QualifiedName , effectiveElement ) ;
921
922
var originalPropertyName = propertyName ;
922
923
if ( propertyName == typeModel . Name )
@@ -934,9 +935,9 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
934
935
OriginalPropertyName = originalPropertyName ,
935
936
Type = substitute ? . Type ?? CreateTypeModel ( element . ElementSchemaType , elementQualifiedName ) ,
936
937
IsNillable = element . IsNillable ,
937
- IsNullable = item . MinOccurs < 1.0m || ( item . XmlParent is XmlSchemaChoice ) ,
938
+ IsNullable = item . MinOccurs < 1.0m || isNullableByChoice ,
938
939
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 ) ,
940
941
FixedValue = element . FixedValue ,
941
942
XmlNamespace = ! string . IsNullOrEmpty ( effectiveElement . QualifiedName . Namespace ) && effectiveElement . QualifiedName . Namespace != typeModel . XmlSchemaName . Namespace
942
943
? effectiveElement . QualifiedName . Namespace : null ,
@@ -969,7 +970,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
969
970
OwningType = typeModel ,
970
971
Name = "Any" ,
971
972
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 ) ,
973
974
IsCollection = item . MaxOccurs > 1.0m || particle . MaxOccurs > 1.0m , // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx
974
975
IsAny = true ,
975
976
XmlParticle = item . XmlParticle ,
@@ -1020,6 +1021,27 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
1020
1021
return properties ;
1021
1022
}
1022
1023
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
+
1023
1045
private NamespaceModel CreateNamespaceModel ( Uri source , XmlQualifiedName qualifiedName )
1024
1046
{
1025
1047
NamespaceModel namespaceModel = null ;
0 commit comments