472,125 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

xs:choice

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>

When I use this type in XML, I want to specify either <Interval> or
<DailyAt> but not both, hence the use of <xs:choice>. However, when I create
a DataSet generated by VS.NET 2003, and try to load my XML file with
..Read(), I get System.Data.ConstraintException Failed to enable constraints.
It seems the framework tries to load both values. So when I use something
like this:

<SchedulingMethod>
<Interval>PT30M</Interval>
</SchedulingMethod>

The error tells that value DailyAt is missing (i.e. it throws
InvalidCastException trying to convert null into TimeSpan).

Am I doing something wrong? Doesn't <xs:choice> mean "only one from the
list" ?

Thanks in advance,

-Oleg.
Nov 12 '05 #1
1 2839
Hi Oleg,

I have to admit, I stared at this for a while without seeing it, but the
solution is fairly simple.

<xs:complexType name="SchedulingMethodType">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:element name="Interval" type="xs:duration" minOccurs="0"
maxOccurs="1" />
<xs:element name="DailyAt" type="xs:duration" minOccurs="0"
maxOccurs="1" />
</xs:choice>
</xs:complexType>

Notice the minOccurs attribute for the Interval and DailyAt elements inside
the choice element. When you load this schema into a dataset, the column
definitions in the table will be specified with "allowNull" and
"AllowDBNull" both set to True. Without setting minOccurs to 0 (zero), the
"allowNull" and "AllowDBNull" properties are set to false.

I hope this helps.

Brooks
[MSFT]
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/copyright.htm.

"Oleg Ogurok" <ol**@ogurok.com.ihatespammers.ireallydo.co> wrote in message
news:10*************@corp.supernews.com...
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>

When I use this type in XML, I want to specify either <Interval> or
<DailyAt> but not both, hence the use of <xs:choice>. However, when I create a DataSet generated by VS.NET 2003, and try to load my XML file with
.Read(), I get System.Data.ConstraintException Failed to enable constraints. It seems the framework tries to load both values. So when I use something
like this:

<SchedulingMethod>
<Interval>PT30M</Interval>
</SchedulingMethod>

The error tells that value DailyAt is missing (i.e. it throws
InvalidCastException trying to convert null into TimeSpan).

Am I doing something wrong? Doesn't <xs:choice> mean "only one from the
list" ?

Thanks in advance,

-Oleg.

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sergey Poberezovskiy | last post: by
2 posts views Thread by Sergey Poberezovskiy | last post: by
2 posts views Thread by hooomee | last post: by
reply views Thread by =?Utf-8?B?RGFtaWFu?= | last post: by
reply views Thread by Peter Larsen | last post: by
reply views Thread by leo001 | last post: by

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.