From 3bbbd0694524e5fe8096e3186780b36414fc997a Mon Sep 17 00:00:00 2001 From: John Welch Date: Thu, 4 Oct 2018 09:52:22 +0100 Subject: [PATCH] Emit XmlElementAttribute for substitutionGroups with an abstract head. See https://github.com/mganss/XmlSchemaClassGenerator/issues/36#issuecomment-426937573 --- XmlSchemaClassGenerator/ModelBuilder.cs | 5 +++++ XmlSchemaClassGenerator/TypeModel.cs | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index 93d21794..b2184269 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -96,6 +96,11 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set) if (type is ClassModel classModel) { classModel.Documentation.AddRange(GetDocumentation(rootElement)); + if (!rootElement.SubstitutionGroup.IsEmpty) + { + classModel.IsSubstitution = true; + classModel.SubstitutionName = rootElement.QualifiedName; + } } type.RootElementName = rootElement.QualifiedName; diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index f2d4a27d..67b3526b 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -225,6 +225,7 @@ public class ClassModel : TypeModel public bool IsAbstract { get; set; } public bool IsMixed { get; set; } public bool IsSubstitution { get; set; } + public XmlQualifiedName SubstitutionName { get; set; } public TypeModel BaseClass { get; set; } public List Properties { get; set; } public List Interfaces { get; set; } @@ -974,7 +975,7 @@ private IEnumerable GetAttributes(bool isArray) foreach (var derivedType in derivedTypes) { var derivedAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlElementAttribute), Configuration.CodeTypeReferenceOptions), - new CodeAttributeArgument(new CodePrimitiveExpression(derivedType.XmlSchemaName.Name)), + new CodeAttributeArgument(new CodePrimitiveExpression((derivedType.SubstitutionName ?? derivedType.XmlSchemaName).Name)), new CodeAttributeArgument("Type", new CodeTypeOfExpression(derivedType.GetReferenceFor(OwningType.Namespace, false)))); if (Order != null) {