From c3b841e4420928be71f78d752b1ac0a2623203ef Mon Sep 17 00:00:00 2001 From: Zoey Goetzinger Date: Wed, 21 Aug 2019 11:16:48 -0500 Subject: [PATCH 1/2] Add support for hexBinary types with a default value. --- .../xsd/simple/simple.xsd | 1 + XmlSchemaClassGenerator/TypeModel.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/XmlSchemaClassGenerator.Tests/xsd/simple/simple.xsd b/XmlSchemaClassGenerator.Tests/xsd/simple/simple.xsd index fdc941b2..a616bcb8 100644 --- a/XmlSchemaClassGenerator.Tests/xsd/simple/simple.xsd +++ b/XmlSchemaClassGenerator.Tests/xsd/simple/simple.xsd @@ -20,6 +20,7 @@ + diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 9fa43b47..af2cf882 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1281,6 +1281,22 @@ public override CodeExpression GetDefaultValueFor(string defaultString, bool att else return new CodePrimitiveExpression(Convert.ChangeType(defaultString, ValueType)); } + else if(type == typeof(byte[]) && !string.IsNullOrWhiteSpace(defaultString)) + { + int numberChars = defaultString.Length; + if(numberChars % 2 == 1) + throw new NotSupportedException(string.Format("Default value {0} is not a valid hexBinary value.", defaultString)); + + var byteValues = new CodePrimitiveExpression[numberChars / 2]; + for (int i = 0; i < numberChars; i += 2) + byteValues[i / 2] = new CodePrimitiveExpression(Convert.ToByte(defaultString.Substring(i, 2), 16)); + + // For whatever reason, CodeDom will not generate a semicolon for the assignment statement if CodeArrayCreateExpression + // is used alone. Casting the value to the same type to work around this issue. + var rv = new CodeCastExpression(typeof(byte[]), new CodeArrayCreateExpression(typeof(byte), byteValues)); + return rv; + + } return new CodePrimitiveExpression(Convert.ChangeType(defaultString, ValueType, CultureInfo.InvariantCulture)); } From 802ee476b7336699d9f859ebd79809ad94a7cdbf Mon Sep 17 00:00:00 2001 From: Zoey Goetzinger Date: Wed, 21 Aug 2019 12:20:07 -0500 Subject: [PATCH 2/2] Removed unreachable code. --- XmlSchemaClassGenerator/TypeModel.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index af2cf882..47514f3f 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1284,9 +1284,6 @@ public override CodeExpression GetDefaultValueFor(string defaultString, bool att else if(type == typeof(byte[]) && !string.IsNullOrWhiteSpace(defaultString)) { int numberChars = defaultString.Length; - if(numberChars % 2 == 1) - throw new NotSupportedException(string.Format("Default value {0} is not a valid hexBinary value.", defaultString)); - var byteValues = new CodePrimitiveExpression[numberChars / 2]; for (int i = 0; i < numberChars; i += 2) byteValues[i / 2] = new CodePrimitiveExpression(Convert.ToByte(defaultString.Substring(i, 2), 16));