Skip to content

Commit

Permalink
Merge pull request #123 from zoey-ada/hexBinary-type
Browse files Browse the repository at this point in the history
Add support for hexBinary types with a default value.
  • Loading branch information
mganss authored Aug 23, 2019
2 parents dee1ac0 + 802ee47 commit dc76f8b
Show file tree
Hide file tree
Showing 2 changed files with 14 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
13 changes: 13 additions & 0 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,19 @@ 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;
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 dc76f8b

Please sign in to comment.