Bug 2907 - XML "choice" not correclty processed during deserialization
Summary: XML "choice" not correclty processed during deserialization
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: 2.10.x
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Lluis Sanchez
Depends on:
Reported: 2012-01-16 01:35 UTC by Josef Semmler
Modified: 2012-12-12 03:52 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

example for the choice issue (4.72 KB, application/x-zip-compressed)
2012-01-18 06:07 UTC, Josef Semmler
Fixes deserialization of choice with ordered read. (59.73 KB, patch)
2012-11-01 05:14 UTC, Roman
Fix for failed tests (3.54 KB, patch)
2012-12-11 08:56 UTC, Roman

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Josef Semmler 2012-01-16 01:35:34 UTC
I have a xml schema file which uses a "choice".
I used xsd to generate a class file for that schema.

When i now try to deserialize a XML file based on that schema into a class instance i get the following error message
Unhandled Exception: System.InvalidOperationException: There is an error in XML document. ---> System.InvalidOperationException: Read by order only possible for encoded/bare format
  at System.Xml.Serialization.ClassMap.GetElement (Int32 index) [0x00000] in <filename unknown>:0
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readBySoapOrder) [0x00000] in <filename unknown>:0
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob) [0x00000] in <filename unknown>:0
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType) [0x00000] in <filename unknown>:0
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType) [0x00000] in <filename unknown>:0
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) [0x00000] in <filename unknown>:0
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00000] in <filename unknown>:0
  at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
The code itself works: It works with microsoft .net and works also pretty well with with mono 2.6.7 ... but it seems that all mono version after 2.6.7 (2.8, 2.10) are no longer able to process the "choice" ?
Comment 1 Miguel de Icaza [MSFT] 2012-01-16 14:56:59 UTC

Would you mind providing a self-contained test case that exhibits the problem?

Please reopen the bug with the test case.
Comment 2 Josef Semmler 2012-01-18 06:07:50 UTC
Created attachment 1223 [details]
example for the choice issue

The attached files contains two important files:

test2.xsd, which is the used schema (very simple)
and test2.cs, which is the generated class file from the xsd. This file has been generated using
xsd test2.xsd /c /order

Important: The issue with mono only occurs, if the option "/order" was used - if this option is not used, the test program run's without problems, too.
Comment 3 Josef Semmler 2012-02-23 05:55:39 UTC
requested information was attached
Comment 4 Nicolas Raoul 2012-08-17 06:35:42 UTC
I wanted to use to DotCMIS library for a multiplatform application, but it uses <xsd:choice> so Mono crashes.

I also confirm Josef's test case reproduces the bug.

Mono JIT compiler version (Debian

It has been 7 months, any idea when this bug will be fixed?
Comment 5 Nicolas Raoul 2012-08-23 07:07:35 UTC
I tried to fix that bug by commenting out the assertion at line 410 of System.Xml.Serialization/XmlTypeMapping.cs

That prevents the exception from happening, but unfortunately the resulting deserialized data is null.
So the problem must be somewhere else.
Comment 6 Josef Semmler 2012-09-13 05:02:02 UTC
@Raoul: If you don't need the latest additions to XmlSerialization, you can work around this issue like this: 

Fetch the mono sources of the version you want (e.g. 2.10.9) - and the sources of mono-2.6.7 (which is IMHO the last working version without that defect!). Extract both source trees - then replace all files in mcs/class/System.XML/System.Xml.Serialization with the files from 2.6.7 ... configure and compile.

It is a stupid and dangerous workaround - but it seems to work for me.
Comment 7 Nicolas Raoul 2012-09-13 05:17:43 UTC
@Josef: Thanks for the tip! Unfortunately, I am developing a Mono app intended to be downloaded by many users, and I can't ask them to mess with their system.

I am now using a workaround: I removed all "Order" parameters/lines in the code generated by Visual C# (actually by the WSDL "Add Service" feature, which probably uses xsd.exe). It seems to be working so far.
Comment 8 Nicolas Raoul 2012-09-13 06:13:34 UTC
I tested more, and I get this error:

There is an error in XML document.
at System.Xml.Serialization.XmlSerializer.Deserialize

So I guess I can't do without Order :-/
Which means I desperately need to fix this bug.
Comment 9 Nicolas Raoul 2012-09-14 05:24:46 UTC
I can confirm that Josef's hack works.
For anyone willing to use Mono for XML or web services, here is how to fix the problem on Ubuntu:

mkdir mono-fix2907
cd mono-fix2907
sudo apt-get install devscripts fakeroot
sudo apt-get build-dep mono-runtime
export DEB_BUILD_OPTIONS="debug nostrip noopt"
fakeroot apt-get source -b mono-runtime
rm -rf mono-
cd ..
mkdir mono-2-6-7
cd mono-2-6-7
git clone git://github.com/mono/mono.git
git checkout mono-2-6-7
cd ../mono-fix2907
cp -r ../mono-2-6-7/mcs/class/System.XML/System.Xml.Serialization mono-
cd mono-
dpkg-buildpackage -rfakeroot -uc -b
cd ..
sudo dpkg -i --force-overwrite *.deb
Comment 10 Roman 2012-11-01 05:14:54 UTC
Created attachment 2828 [details]
Fixes deserialization of choice with ordered read.

I have added the patch that fixed ordered deserialization of array of choices.
Should work for single choices too.

Sorry, patch also makes line endings consistent (LF) and formats some lines (MSVS-style).

I am not sure it won't break something since the code was not pretty and some things remained unclear. Developers definitely should add some xml-documentation...
Comment 11 Nicolas Raoul 2012-11-02 03:33:55 UTC
I created a pull request from Roman's patch:

I cancelled the formatting changes to make the diff easier to understand:
Comment 12 Miguel de Icaza [MSFT] 2012-11-02 09:44:52 UTC
Lluis, can you review the provided patch on github?

Nicolas/Roman: any chance you guys can provide a test case that we can use to validate the fix?

Comment #2 contains a test case
Comment 13 Nicolas Raoul 2012-11-02 09:54:44 UTC
Miguel, I will write a test case within 3 days, based on the test case in comment 2.
Note: I have not tested the patch yet.
Comment 14 Nicolas Raoul 2012-11-05 03:16:29 UTC
I wrote a test in "mono/tests" and sent a pull request:
I tried to follow the guidelines and patterns observed in other tests.

That might a problem with my setup, but recently I get "mdoc: There is an error in XML document" messages, and a person called Jester explained how it might be due to the patch:
Comment 15 Roman 2012-11-05 09:39:36 UTC
Yep. My mistake. Since all memers in my program are marked with Order, i've never run into it.
Comment 16 Nicolas Raoul 2012-11-06 00:24:58 UTC
I fixed my patch branch, now MDOC and everything build fine.
Pull request: https://github.com/mono/mono/pull/495/commits
Comment 17 Miguel de Icaza [MSFT] 2012-12-07 16:58:20 UTC
Thanks, patch applied!
Comment 18 Roman 2012-12-11 08:56:03 UTC
Created attachment 3080 [details]
Fix for failed tests

I addded one more patch to fix failed tests reported here https://github.com/mono/mono/pull/495 by garuma.
Patch must be applied after the previous one.
Comment 19 Roman 2012-12-12 03:52:35 UTC
Created pull request https://github.com/mono/mono/pull/522