diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index 214ef72..8c61b1f 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -312,9 +312,13 @@ public static XmlQualifiedName GetQualifiedName(this TypeModel typeModel) public static string GetUniqueTypeName(this NamespaceModel model, string name) { var n = name; + var i = 2; - for (var i = 2; model.Types.ContainsKey(n) && model.Types[n] is not SimpleModel; i++) + while (model.Types.ContainsKey(n) && model.Types[n] is not SimpleModel) + { n = name + i; + i++; + } return n; } diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index 5099e38..6a49bf5 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -11,6 +11,9 @@ namespace XmlSchemaClassGenerator { internal class ModelBuilder { + private const string ItemName = "Item"; + private const string PropertyName = "Property"; + private const string ElementName = "Element"; private readonly GeneratorConfiguration _configuration; private readonly XmlSchemaSet _set; private readonly Dictionary> AttributeGroups = new(); @@ -868,12 +871,12 @@ private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchema attributeQualifiedName = new XmlQualifiedName(typeName, owningTypeModel.XmlSchemaName.Namespace); // try to avoid name clashes if (NameExists(attributeQualifiedName)) - attributeQualifiedName = new[] { "Item", "Property", "Element" }.Select(s => new XmlQualifiedName(attributeQualifiedName.Name + s, attributeQualifiedName.Namespace)).First(n => !NameExists(n)); + attributeQualifiedName = new[] { ItemName, PropertyName, ElementName }.Select(s => new XmlQualifiedName(attributeQualifiedName.Name + s, attributeQualifiedName.Namespace)).First(n => !NameExists(n)); } } if (name == owningTypeModel.Name) - name += "Property"; + name += PropertyName; } name = owningTypeModel.GetUniquePropertyName(name, properties); @@ -989,7 +992,7 @@ private PropertyModel PropertyFromElement(TypeModel owningTypeModel, XmlSchemaEl var name = _configuration.NamingProvider.ElementNameFromQualifiedName(effectiveElement.QualifiedName, effectiveElement); var originalName = name; if (name == owningTypeModel.Name) - name += "Property"; // member names cannot be the same as their enclosing type + name += PropertyName; // member names cannot be the same as their enclosing type name = owningTypeModel.GetUniquePropertyName(name, properties); @@ -1023,7 +1026,7 @@ private XmlQualifiedName GetQualifiedName(TypeModel typeModel, XmlSchemaParticle elementQualifiedName = new XmlQualifiedName(typeName, typeModel.XmlSchemaName.Namespace); // try to avoid name clashes if (NameExists(elementQualifiedName)) - elementQualifiedName = new[] { "Item", "Property", "Element" }.Select(s => new XmlQualifiedName(elementQualifiedName.Name + s, elementQualifiedName.Namespace)).First(n => !NameExists(n)); + elementQualifiedName = new[] { ItemName, PropertyName, ElementName }.Select(s => new XmlQualifiedName(elementQualifiedName.Name + s, elementQualifiedName.Namespace)).First(n => !NameExists(n)); } } diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 93cb45e..be85c74 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -385,11 +385,13 @@ public override CodeTypeDeclaration Generate() if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed)))) { var propName = "Text"; + var propertyIndex = 1; // To not collide with any existing members - for (var propertyIndex = 1; Properties.Exists(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++) + while (Properties.Exists(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal)) { propName = $"Text_{propertyIndex}"; + propertyIndex++; } // hack to generate automatic property var text = new CodeMemberField(typeof(string[]), propName + PropertyModel.GetAccessors()) { Attributes = MemberAttributes.Public };