2

Apologies if this is a silly question, I haven't really done anything with xml up until now.

I want to be able to deserialise some xml into a class that has a property of type object. This property could be an int, guid or a string. In the xml I would like this property to be defined as an attribute. I would have expected my xsd to look something like this:

<xsd:complexType name="MyClass">
  <xsd:attribute name="MyProperty" type="xsd:anySimpleType" />
</xsd:complexType>

However, visual studio intellisense doesn't list the option, "xsd:anySimpleType". Is this an issue with intellisense or am I doing something inherently wrong?

ajbeaven
  • 9,265
  • 13
  • 76
  • 121

2 Answers2

4

Although it's legal to have an attribute of type xs:anySimpleType, I would avoid it and use xs:string instead. This is because the spec leaves many questions open about how such an attribute should behave. It's not possible to restrict it using an enumeration or a regular expression, it's very poorly defined what happens if you use it in an identity constraint, etc. The validation is exactly the same as for xs:string, but other aspects of the behaviour are fairly weird and likely to vary between implementations.

One could argue that xs:anySimpleType makes sense if you want later to define subtypes such as string, int, and URI. But that's the only case I can think of in favour.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
3

The xsd:anySimpleType (and xsd:anyType) are special so-called ur-type definitions (the former being the simple ur-type); incidentally, when it comes to XSD datatypes, I always point out this diagram.

Just being this special, I believe is part of the reason why these types don't show up in Intellisense. I guess another reason is also that it is the type assigned by default; in other words, for your attribute if you do not set the type, it will be anySimpleType regardless.

However, I also admit this explanation doesn't work with other editors - Eclipse being one of them; There you get all these types in Intellisense, no questions asked...

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62