Dan Digs In To Serialization

Dan's getting some welcome assistance from Scott Hanselman with some XML Serialization troubles.

I've been occasionally sticking my oar in to help Dan out with this puzzling little foible of the .NET XML serialization stack for a couple of weeks now, and I can say it really does look like a bug to me. At root, we've got a perfectly valid, normative XML Schema that declares an unambiguous XML vocabulary; and we've got a set of classes output from xsd.exe based upon that schema, that the XML Serializer serializes to XML documents which don't always conform to the original schema. That's got to be a bug. But where? Is it in xsd.exe? Is it in the classes xsd.exe generates? Is it in the serializer that generates the custom serialization code based on those classes? Or is it in the classes that the serializer generates? Or is it in Dan's code?

Now today, with Scott's help, Dan's managed to narrow down what's going on to the ordering of a series of if statements in the serialization code generated by XmlSerializer the first time you ask it to initialise itself for a particular type.

What’s immediately apparent is that neither set of if statements here match the order in which the stack is written in the source code and, ironically, the if statement for hello is at the bottom of the stack in the right hand pane where the elementattribute for hello was on the top of the stack in the original code. To see if this was an arbitrary placing of the if statements in the serializer dll, I moved the hello attribute up the stack one place at a time, rebuilt and viewed the temp dll code and the order of the ifs never matched the attribute stack. More annoyingly, I didn't (through luck or otherwise) get the if statement for hello to appear as the last of the if statements in the DLL again which would produce exactly the required results.
[Via Dan's Archive]

Now this rings a bell. Specifically, not too long ago K. Scott Allen (everyone's called Scott today) blogged about how "Microsoft decided to introduce some randomization in reflection methods like GetFields". Well, deep down inside the XmlSerializer (and a deep-dive expedition with Lutz Roeder's Reflector confirms this), we find some calls to GetCustomAttributes() are populating the arrays that ultimately get iterated over to generate those if statements. How else is it going to analyse the type it's been handed to generate that serialization code but by using reflection?

Now granted, there's not anything explicit in the documentation for MemberInfo.GetCustomAttributes() that indicates that it deliberately shuffles the cards before handing them back to you (and reflector draws a blank at the managed/unmanaged code boundary inside System.Reflection), but it would be consistent to assume that it is not going to return them in sourcecode order. There's little to suggest that we should assume sourcecode order would even be preserved by the C# compiler in constructing the MSIL.

Which leaves us back at square one, with no way to have any guaranteed influence over exactly what order those if statements get generated in. We can't, then, blame xsd.exe for the order it puts the attributes in - that doesn't seem to make that much difference to the result. We can maybe blame the XML Serializer for not caring that the order of its if statements is significant.

Let's hope Dan gets a resolution to this one soon - it'll be interesting to see what he finds.

Print | posted on Thursday, May 25, 2006 5:03 PM

Comments on this post

# Subtle Behaviors in the XML Serializer can kill

Requesting Gravatar...
Left by ComputerZen.com - Scott Hanselma on May 25, 2006 5:32 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
That does look nasty, really nasty. Bug nasty!
Left by Scott on May 25, 2006 7:40 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
If you are talking about what I think you are talking about...
XML element attribute lists are not required to have fixed order following parsing (in the XML spec). Since attributes have to have unique names (per element) they typically get pushed into a hash, not an array. Order is lost.
That's one reason why people change those attributes to being child elements in a lot of apps where that data order matters.
Left by Anon on Jul 09, 2006 4:35 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
My weakpoint is debugging, its great to get some solice in the fact that I am not alone in having troubles with this. Sometimes it can take me days to diagnose a problem with the code :(
Left by rakeback on Feb 03, 2010 2:26 PM

# asdad

Requesting Gravatar...
The World Trade Organization and the People's Republic of China emerged as a prominent destination for production offshoring.
Left by Offshore Transcription on Feb 17, 2010 9:29 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
The serialization proxy typically employs a static, private nested classthat is serialized in place of the class through writeReplace.. The proxy itself stores all the information needed to reconstitute the class being serialized.
Left by primes des jeux de casino on Feb 22, 2010 11:01 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Thanks mate. This is such good info for my research. I will bookmark your post here on Digg.
Left by Substance Abuse on Mar 12, 2010 2:26 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Foreign exchange brokers did large amounts of business facilitating interbank trading and matching anonymous counterparts for small fees.

Left by forex scam on Mar 17, 2010 7:50 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
There are relly cheap cds available here and many good links to download some free music from our sitte.
Left by cheap music downloads on Mar 19, 2010 1:52 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
A self-contained resort attempts to provide for most of a vacationer's wants while remaining on the premises such as food,drink,lodging,sports, entertainment and shopping.
Left by travel deals on Mar 20, 2010 10:56 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Luncheon Voucher is a paper ticket used by some employees in the United Kingdom to pay for meals in private restaurants

Left by Pizza Express Vouchers on Mar 26, 2010 11:13 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Requesting Gravatar...
My weakpoint is debugging, its great to get some solice in the fact that I am not alone in having troubles with this. Sometimes it can take me days to diagnose a problem with the code :(
Left by www.mousse-coiffante.de on Mar 28, 2010 5:15 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Surprisingly,client-centered therapy proved most effective.It has been argued,however these findings may be attributable to the profound difference in therapist outlook between the two-factor and client-centered approaches, rather than to client-centered techniques per se.

Left by Restaurant Vouchers on Mar 29, 2010 2:47 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
I just wanna thank you for sharing your information and your site or blog this is simple but nice article I've ever seen i like it i learn something today.
Left by Copyright Music on Mar 30, 2010 6:08 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
You made some good points there.I did a search on the topic and found most people will agree with your blog.Thanks
Left by DVD Replication on Mar 30, 2010 6:20 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
Left by Children Movies on Mar 30, 2010 6:38 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on. You have done a marvellous job!
Left by Car DVD on Mar 30, 2010 6:46 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
The people who made this should set the example for what humankind should be and represent. Purely perfect amazing work....
Left by gioca ai casinò online on Mar 30, 2010 12:21 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
I'll bet that the coupons for Old Dells are great. I would like a coupon for a '96 vintage Optiplex. Heck I'll even spring for the 3x CD-ROM drive and a copy of Windows 95 (OEM Service Release 2 of course).
Left by Werkstoffprüfung on Mar 31, 2010 12:25 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Its always good to learn tips like you share for blog posting. As I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too.
Left by learn futures trading on Apr 05, 2010 6:16 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Its always good to learn tips like you share for blog posting. As I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too.
Left by watches replicas on Apr 29, 2010 2:55 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Very interesting Article, thanks a lot! I would like to see if maybe the base64 decode function plays some significant difference, so, what happens if you leave the image away? same results on Android devie?
Best Regards
Left by vincere di più al casinò on Apr 29, 2010 8:55 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Very interesting article..
I enjoyed reading it.
I must say I liked the name of your blog too.
Left by Bratz on May 07, 2010 2:35 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Well..,client-centered therapy proved most effective.It has been argued,however these findings may be attributable to the profound difference in therapist outlook between the two-factor and client-centered approaches, rather than to client-centered techniques per se.
Left by adsense secret on May 10, 2010 4:10 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
I'm also going to blog about that. Your time isn't going to waste with your posts.
Left by Digital photo cameras on May 22, 2010 9:21 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Thanks a lot for sharing, very helpful Article..
Left by Boxfresh on May 31, 2010 3:27 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Its a pleasure reading your post.
Left by The Employment Law Group on Jun 05, 2010 1:26 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.
Left by Drug Rehab Florida on Jun 08, 2010 6:40 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Its a pleasure reading your post.
Good share,Attractive enough for me ,thanks for your kind share
Left by dvd on Jun 08, 2010 8:42 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Thanks. I get some useful information about this. Good work.
Left by Fishing Hats on Jun 09, 2010 3:36 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Dan Digs is amazing
Left by Gambler on Jun 21, 2010 8:06 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Aw, it was a top quality content. Actually I would like to write like this as well - taking time and real energy to bring about an excellent post... however what can I say... I procrastinate an awful lot and by no means appear to get things completed...
Left by simulation assurance auto on Jun 24, 2010 8:00 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Nice blog with very strong content. I love reading it over again.
Left by Rehab New Jersey on Jul 02, 2010 9:06 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...
Lots of great information and inspiration, both of which I like. I am also glad to know the way to solve with some XML Serialization troubles.
Left by Custom Remodelers Circle Pines on Jul 12, 2010 9:56 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
I always like your blog post because you always comes with different ideas and information. I always shared your site post with my friends. Keep posting and i will follow you..
Left by internet marketing companies on Jul 14, 2010 5:51 AM

# re: Dan Digs In To Serialization

Requesting Gravatar...

I only lately started leaving comments! Makes me think a tiny far more about what I enjoyed and learned in regards to the

write-up! Outstanding points, thanks!
Left by arnettetou on Aug 03, 2010 8:47 PM

# re: Dan Digs In To Serialization

Requesting Gravatar...
This is great news. really looking forward to see more of it.
Left by Rasenmäher Roboter on Sep 01, 2010 7:38 AM

Your comment:

 (will show your gravatar)
 
Please add 8 and 2 and type the answer here: