From b918fa975df44a359d6ca80773ce6bef54aa34fd Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Tue, 12 Jan 2021 17:44:19 +0100 Subject: [PATCH] Do not emit DefaultValueAttribute for xml:lang and xml:space attributes (see #245) --- XmlSchemaClassGenerator/CodeUtilities.cs | 10 ++++++++++ XmlSchemaClassGenerator/TypeModel.cs | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index 52fa7f19..6bbbdd6e 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -399,5 +399,15 @@ public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfigurati else return new CodeTypeReference(t, conf.CodeTypeReferenceOptions); } + + /// + /// See https://github.com/mganss/XmlSchemaClassGenerator/issues/245 + /// and https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributeattribute#remarks + /// + public static bool IsXmlLangOrSpace(XmlQualifiedName name) + { + return name != null && name.Namespace == "http://www.w3.org/XML/1998/namespace" + && (name.Name == "lang" || name.Name == "space"); + } } } \ No newline at end of file diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 8741f351..a46d918d 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -813,7 +813,8 @@ public void AddInterfaceMembersTo(CodeTypeDeclaration typeDeclaration) { var defaultValueExpression = propertyType.GetDefaultValueFor(DefaultValue, IsAttribute); - if ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression)) + if ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression) + && !CodeUtilities.IsXmlLangOrSpace(XmlSchemaName)) { var defaultValueAttribute = CreateDefaultValueAttribute(typeReference, defaultValueExpression); member.CustomAttributes.Add(defaultValueAttribute); @@ -927,7 +928,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi member.Name += GetAccessors(member.Name, backingField.Name, propertyType.GetPropertyValueTypeCode(), false, withDataBinding); - if (IsNullable && ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression))) + if (IsNullable && ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression)) + && !CodeUtilities.IsXmlLangOrSpace(XmlSchemaName)) { var defaultValueAttribute = CreateDefaultValueAttribute(typeReference, defaultValueExpression); member.CustomAttributes.Add(defaultValueAttribute);