473,803 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer bug for xs:choice?

(This is using .net version 2.0.50727 and system.xml 2.0.50727.42)

I am getting an error when serializing using classes generated by xsd.exe.
The error is of the type "The Type <typewas not expected. Use the
XmlInclude or SoapInclude etc"
This error is appearing sprodically, with no obvious pattern. When it
occurs, it happens every time we run the code, however changes to our code in
creating the objects before they are serialized sometimes make it go away
(again, no obvious pattern).
Also, we have taken the same code and data, compiled it on two machines
built from the same image (but probably a bit dirty as they have been used by
devs for a while, although no significant changes in software versions, .net
framework, vs etc), and one has worked and one hasn't.
A bit more depth>>>>>>

The schema contains a choice, of which one of the choices is an element with
no child elements or attributes.
E.g.

<xs:element name="Animal">
<xs:complexType >
<xs:choice>
<xs:element name="Dog">
<xs:complexType >
<xs:attribute name="Name" type="xs:string "/>
</xs:complexType>
</xs:element>
<xs:element name="Cow">
<xs:complexType >
<xs:attribute name="Name" type="xs:string "/>
</xs:complexType>
</xs:element>
<xs:element name="Pig">
</xs:element>
<xs:element name="Sheep">
<xs:complexType >
<xs:attribute name="Name" type="xs:string "/>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
This has generated the following class (along with lots of others from the
farmyard schema ;) ) using xsd.exe

public partial class Animal
{

private object itemField;

[System.Xml.Seri alization.XmlEl ementAttribute( "Pig", typeof(object))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Dog", typeof(AnimalDo g))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Cow", typeof(AnimalCo w))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Sheep", typeof(AnimalSh eep))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}

Interestingly, the generated code shown above is slightly different for this
case (choice which contains only 1 element with no child elements or
attributes) than for another choice in the schema which contains more than 1
element that has no child elements or attributes. In that scenario an extra
field is generated called ItemChoiceType and there is an extra attribute on
Item (XmlChoiceIdent ifierAttribute) . ??I believe this is significant as the
runtime generated serializer code handles it differently??

I have looked at the runtime generated serializer code at the offending
method...
void Write11_Animal( some params incl. global::Farmyar dNamespace.Anim al o)
{
...

WriteStartEleme nt(..);

...

if (o.@Item is global::Farmyar dNamespace.Dog)
{
Write10_Item(@" Dog", ... some more params);
}
else if (o.@Item is global::System. Object)
{
Write2_Object(@ "Pig", ... some more params);
}
else if (o.@Item is global:Farmyard Namespace.Cow)
{
Write9_Item(@"C ow", ... some more params);
}
else if (o.@Item is global:Farmyard Namespace.Sheep )
{
Write8_Item(@"S heep", ... some more params);
}

...

WriteEndElement (...);
}

As you can see, if the o parameter is a Cow, it will still get caught by the
"o@Item is global::System. Object" clause, and then doesn't get caught by any
of the clauses in the big if statement in Write2_Object method (not shown),
and eventually causes it to fall over in WriteTypedPrimi tive method.

I'm not sure what causes the runtime generated serializer code to determine
the order of the if statement shown above, but could explain the problems
with it working at different points in time and on different machines.
There is a very simple workaround to this... add a dummy attribute to the
single element with no child elements or attributes.
In our situation, although it is a complete hack, it is actually a viable
thing to do. However, if there is a "nice" way to fix this, then please could
somebody tell me, or is it a real bug?

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/communities...lic.dotnet.xml
Feb 1 '07 #1
0 2145

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
510
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of course, the generated proxy is not the same). When I am looking at the C++ generated code, it seems fine, but when I am executing the code, I always get the first choice type.
1
2915
by: Oleg Ogurok | last post by:
Hi all, I have a complex type defined as follows: <xs:complexType name="SchedulingMethodType"> <xs:choice maxOccurs="1"> <xs:element name="Interval" type="xs:duration" /> <xs:element name="DailyAt" type="xs:duration" /> </xs:choice> </xs:complexType>
4
1731
by: Sergey Poberezovskiy | last post by:
Hi, As part of my schema I need to ensure that at least one of two fields have values. I defined my schema as follows: .... <xs:choice> <xs:element ref="el1"/> <xs:element ref="el2"/> <xs:sequence> <xs:element ref="el1"/>
2
4356
by: Sergey Poberezovskiy | last post by:
Hi, If I define my schema as <xs:choice> <xs:sequence> <xs:element ref="el_1" /> <xs:element ref="el_2" /> </xs:sequence> <xs:sequence>
2
5664
by: pawel.pabich | last post by:
Hajo, I know only one way of forcing element to have one or more childes: <xs:element name="saleOrSupplyMethod"> <BR/> <xs:complexType> <xs:choice> <xs:sequence> <xs:element name="generalSale" type="emptyTagType" /> <xs:element name="prescription" type="emptyTagType"/>
7
2459
by: rbarschaw | last post by:
I have the following schema designed: <xs:complexType name="AzzFeature-BoxType" mixed="true"> <xs:choice minOccurs="1" maxOccurs="unbounded"> <xs:element ref="Sub-Head" minOccurs="1" maxOccurs="unbounded"/> <xs:element ref="Title-Text" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="Text" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="Link-List" minOccurs="0" maxOccurs="unbounded"/> </xs:choice> <xs:attribute...
2
1673
by: hooomee | last post by:
Given: <xs:choice maxOccurs=5> <xs:element name="Foo" type="bar" /> <xs:element name="Foo1" type="bar" /> <xs:element name="Foo2" type="bar" /> </xs:choice> Is the choice made once and then the choosen element can occur 5 times, or is the choice made once for each occurance?
2
1356
by: cmay | last post by:
I am beginning to wonder if it is not possible to get this working. I am trying to do: <root> <a/> <b/> <c/> </root>
0
2842
by: Peter Larsen | last post by:
Is this really a valid schema design? <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:choice> <xs:element name="e1" minOccurs="0" />
0
9565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.