@@ -985,9 +985,22 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
985985 Substitute substitute = null , int order = 0 , bool passProperties = true )
986986 {
987987 var properties = new List < PropertyModel > ( ) ;
988+ var initialOrder = order ;
989+ XmlSchemaObject prevParent = null ;
990+ int batchStart = - 1 ;
988991
989992 foreach ( var item in items )
990993 {
994+ // Track parent changes to detect transitions between choice branches.
995+ // When elements from a new branch are encountered, batchStart marks where
996+ // the new branch's properties begin in the list.
997+ if ( item . XmlParent != prevParent )
998+ {
999+ if ( prevParent != null )
1000+ batchStart = properties . Count ;
1001+ prevParent = item . XmlParent ;
1002+ }
1003+
9911004 PropertyModel property = null ;
9921005
9931006 switch ( item . XmlParticle )
@@ -1023,20 +1036,44 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
10231036 // Discard duplicate property names. This is most likely due to:
10241037 // - Choice or
10251038 // - Element and attribute with the same name
1026- if ( property != null && ! properties . Exists ( p => p . Name == property . Name ) )
1039+ if ( property != null )
10271040 {
1028- var itemDocs = GetDocumentation ( item . XmlParticle ) ;
1029- property . Documentation . AddRange ( itemDocs ) ;
1030-
1031- if ( _configuration . EmitOrder )
1032- property . Order = order ++ ;
1041+ var existingIndex = properties . FindIndex ( p => p . Name == property . Name ) ;
1042+ if ( existingIndex >= 0 )
1043+ {
1044+ // Duplicate found in a choice branch - move the current batch of
1045+ // new-branch properties to just before the duplicate's position
1046+ // so that the interleaved order across branches is preserved.
1047+ if ( batchStart >= 0 && batchStart < properties . Count )
1048+ {
1049+ var count = properties . Count - batchStart ;
1050+ var segment = properties . GetRange ( batchStart , count ) ;
1051+ properties . RemoveRange ( batchStart , count ) ;
1052+ var adjustedIndex = existingIndex >= batchStart ? existingIndex - count : existingIndex ;
1053+ properties . InsertRange ( adjustedIndex , segment ) ;
1054+ }
1055+ batchStart = properties . Count ;
1056+ }
1057+ else
1058+ {
1059+ var itemDocs = GetDocumentation ( item . XmlParticle ) ;
1060+ property . Documentation . AddRange ( itemDocs ) ;
10331061
1034- property . IsDeprecated = itemDocs . Exists ( d => d . Text . StartsWith ( "DEPRECATED" ) ) ;
1062+ property . IsDeprecated = itemDocs . Exists ( d => d . Text . StartsWith ( "DEPRECATED" ) ) ;
10351063
1036- properties . Add ( property ) ;
1064+ properties . Add ( property ) ;
1065+ }
10371066 }
10381067 }
10391068
1069+ // Reassign Order values based on final property positions,
1070+ // accounting for any reordering due to choice branch interleaving.
1071+ if ( _configuration . EmitOrder )
1072+ {
1073+ for ( var i = 0 ; i < properties . Count ; i ++ )
1074+ properties [ i ] . Order = initialOrder + i ;
1075+ }
1076+
10401077 return properties ;
10411078 }
10421079
0 commit comments