Taco
Taco

Reputation: 2923

XmlAttributeAttribute DataType Property?

I haven't found any exact documentation on this and there's not really much to the question; are we required to use DataType = "string" instead of typeof(string) when working with the XmlAttributeAttribute? For example:

XmlAttribute("Description", DataType = "string")
XmlAttribute("Description", typeof(string))

Both of the lines above compile; however, when using the typeof(string) method, I get an error saying primitive types can't be specified (could this be related to the nesting of classes and overridden properties?).

If you remove the DataType all together, for some reason reflection fails and the build won't succeed.

Am I doing something wrong, or is this the way it is meant to be? If the requirement is to use the DataType method, then how do you specify custom types such as enum?

Upvotes: 0

Views: 710

Answers (1)

V0ldek
V0ldek

Reputation: 10563

The DataType is used for simple types in the xsd namespace, so string for example. For complex types you specify the Type property of the XmlAttributeAttribute. So since xsd:string is a simple type in the xsd namespace, you use

[XmlAttribute("Name", DataType = "string")]

If you had, as you've mentioned, a custom enum, you'd do

[XmlAttribute("Name", typeof(MyEnum))]

Upvotes: 1

Related Questions