Skip to content

Commit

Permalink
Add support for hexBinary types with a default value.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoey Goetzinger committed Aug 21, 2019
1 parent dee1ac0 commit c3b841e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions XmlSchemaClassGenerator.Tests/xsd/simple/simple.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<xs:element name="defaultDateTime" type="xs:dateTime" default="2004-04-12T13:20:00-05:00" />
<xs:element name="defaultDate" type="xs:date" default="2004-04-12" />
<xs:element name="defaultTime" type="xs:time" default="09:30:10Z" />
<xs:element name="defaultHexBinary" type="xs:hexBinary" default="F5FA" />

<xs:element name="anyURIElement" type="xs:anyURI" />
<xs:element name="base64BinaryElement" type="xs:base64Binary" />
Expand Down
16 changes: 16 additions & 0 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit c3b841e

Please sign in to comment.