Skip to content

Commit

Permalink
Use root element name for XmlTypeAttribute if different from class name
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Sep 5, 2023
1 parent b6278be commit 2e2b3de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2617,7 +2617,8 @@ public void TestNetex()
CollectionSettersMode = CollectionSettersMode.Public,
EmitOrder = true,
SeparateSubstitutes = true,
GenerateInterfaces = false
GenerateInterfaces = false,
UniqueTypeNamesAcrossNamespaces = true,
};
var xsdFiles = new[]
{
Expand Down
12 changes: 6 additions & 6 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace XmlSchemaClassGenerator
{
public static class CodeUtilities
{
private static readonly Regex invalidCharsRgx = new Regex("[^_a-zA-Z0-9]");
private static readonly Regex whiteSpace = new Regex(@"(?<=\s)");
private static readonly Regex startsWithLowerCaseChar = new Regex("^[a-z]");
private static readonly Regex firstCharFollowedByUpperCasesOnly = new Regex("(?<=[A-Z])[A-Z0-9]+$");
private static readonly Regex lowerCaseNextToNumber = new Regex("(?<=[0-9])[a-z]");
private static readonly Regex upperCaseInside = new Regex("(?<=[A-Z])[A-Z]+?((?=[A-Z][a-z])|(?=[0-9]))");
private static readonly Regex invalidCharsRgx = new("[^_a-zA-Z0-9]");
private static readonly Regex whiteSpace = new(@"(?<=\s)");
private static readonly Regex startsWithLowerCaseChar = new("^[a-z]");
private static readonly Regex firstCharFollowedByUpperCasesOnly = new("(?<=[A-Z])[A-Z0-9]+$");
private static readonly Regex lowerCaseNextToNumber = new("(?<=[0-9])[a-z]");
private static readonly Regex upperCaseInside = new("(?<=[A-Z])[A-Z]+?((?=[A-Z][a-z])|(?=[0-9]))");

// Credits: chviLadislav
// https://stackoverflow.com/questions/18627112/how-can-i-convert-text-to-pascal-case
Expand Down
11 changes: 8 additions & 3 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ public virtual CodeTypeDeclaration Generate()

protected void GenerateTypeAttribute(CodeTypeDeclaration typeDeclaration)
{
if (XmlSchemaName == null || IsRedefined) return;
var xmlSchemaName = XmlSchemaName;

if (xmlSchemaName == null && RootElementName != null && typeDeclaration.Name != RootElementName.Name)
xmlSchemaName = RootElementName;

if (xmlSchemaName == null || IsRedefined) return;

var typeAttribute = AttributeDecl<XmlTypeAttribute>(
new(new CodePrimitiveExpression(XmlSchemaName.Name)),
new(nameof(XmlRootAttribute.Namespace), new CodePrimitiveExpression(XmlSchemaName.Namespace)));
new(new CodePrimitiveExpression(xmlSchemaName.Name)),
new(nameof(XmlRootAttribute.Namespace), new CodePrimitiveExpression(xmlSchemaName.Namespace)));

// don't generate AnonymousType if it's derived class, otherwise XmlSerializer will
// complain with "InvalidOperationException: Cannot include anonymous type '...'"
Expand Down

0 comments on commit 2e2b3de

Please sign in to comment.