A code-first quiz

You're using ASMX 2.0 in Whidbey. You want to write a service that uses one of these classes:

public class Person1
{
  public string name;

  [XmlAnyElement]
  XmlElement[] any;
}

public class Person2
{
  public string? name;

  [XmlAnyElement]
  XmlElement[] any;
}

public class Person3
{
  [XmlElement(IsNillable=false)]
  public string? name;

  [XmlAnyElement]
  XmlElement[] any;
}

Which of these three variations on a Person class will not violate UPA? If more than one does not violate UPA, rank them based on likelihood of interop problems, with most interoperable first.


Posted Mar 04 2005, 01:54 PM by tim-ewald

Comments

Dare Obasanjo wrote re: A code-first quiz
on 03-04-2005 1:21 PM
This example seems very contrived. The least of which is the fact that I could make the same mistake if I started with the schema especially since some XML schema validators tools don't even catch this error (e.g. XML Spy doesn't think UPA rules make sense so they explicity don't check for them).

I'll respond properly to this in my blog over the weekend.
Tim wrote re: A code-first quiz
on 03-04-2005 1:39 PM
Dare,

Of course this example is contrived, I was just trying to make a point. Lots of XSD tools catch UPA, including the Whidbey XSD editor, and .NET and Java (well Xerces at least) XSD validators. Not enough XSD generators do.

Also, XMLSpy *does* catch UPA if you explicitly validate your document in text view. If you just draw a diagram and save it, it won't enforce the rule. I know they've come out against this requirement, but I'm guessing there was enough push back that they had to implement it.
Tim wrote re: A code-first quiz
on 03-04-2005 3:21 PM
I should add one more thing. The fact that you can create an illegal schema by hand as well as with code-gen is not really the point. The point is that if you create an invalid schema, you have to fix it. If you are working with schema directly, yes, you need to know about UPA. If you are working schema indirectly via code-gen, then you have to know about UPA *and* why your code-gen tool is creating the problem. In other words, you have to know in either case; it's really a question of how much more you have to know.
Erik Johnson wrote re: A code-first quiz
on 03-05-2005 2:45 PM
OK, I'll take a stab. But I don't know enough about how Whidbey generates XSD. BTW, I don't think you can have a "string?" declaration because it is not a value type (maybe you have a newer version of Whidbey?).

Also, the class member "any" could map to an element named "any" that is a complexType containing an xs:any element (0 - unbounded). If that's the case, then none of the examples violate UPA, right?

But I'll assume that the class member "any" maps to an <xs:any /> declaration directly following a "name" element declaration and that the XSD uses xs:sequence. If Whidbey generates the XSD complex types using xs:choice, then all three examples clearly violate UPA.

I'm also assuming the (what should be) IsNullable attribute trumps the nullible "string?" declaration when it comes to generating schema. So that would make Person3 basically look like Person1 from a description standpoint.

So, 1 and 3 do not violate UPA, but the serializer might work differently on each end might act differently (an interop problem). Number 1 in the most interoperable because the class declaration (and intent) best matches the XSD emitted.

Number 2 is a UPA problem because if the first element in the message is named "name", the validator would have to look inside the element to see if the content matches the simpleType (mapped to string) or if it has complex content, which case is a member of the any[] array. This is exactly the problem UPA seeks to avoid.

It's weird, but when I first looked, I thought the examples came from a C# code generator.

Did I get close to winning a prize?

- Erik
Erik Johnson wrote re: A code-first quiz
on 03-05-2005 2:47 PM
OK, I'll take a stab. But I don't know enough about how Whidbey generates XSD. BTW, I don't think you can have a "string?" declaration because it is not a value type (maybe you have a newer version of Whidbey?).

Also, the class member "any" could map to an element named "any" that is a complexType containing an xs:any element (0 - unbounded). If that's the case, then none of the examples violate UPA, right?

But I'll assume that the class member "any" maps to an <xs:any /> declaration directly following a "name" element declaration and that the XSD uses xs:sequence. If Whidbey generates the XSD complex types using xs:choice, then all three examples clearly violate UPA.

I'm also assuming the (what should be) IsNullable attribute trumps the nullible "string?" declaration when it comes to generating schema. So that would make Person3 basically look like Person1 from a description standpoint.

So, 1 and 3 do not violate UPA, but the serializer might work differently on each end might act differently (an interop problem). Number 1 in the most interoperable because the class declaration (and intent) best matches the XSD emitted.

Number 2 is a UPA problem because if the first element in the message is named "name", the validator would have to look inside the element to see if the content matches the simpleType (mapped to string) or if it has complex content, which case is a member of the any[] array. This is exactly the problem UPA seeks to avoid.

It's weird, but when I first looked, I thought the examples came from a C# code generator.

Did I get close to winning a prize?

- Erik

Add a Comment

(required)  
(optional)
(required)  
Remember Me?