473,789 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xsd.exe and choice

Hi there!

I have the following XSD (Well, this is not the real one, but the
concept is the same....)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefa ult="qualified" >
<xs:complexTy pe name="DataItemT ype">
<xs:choice>
<xs:element name="Item1" type="Item1Type "/>
<xs:sequence>
<xs:element ref="Item2"/>
<xs:element ref="Item3"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexTy pe name="Item1Type ">
<xs:sequence>
<xs:element ref="Item1a"/>
<xs:element ref="Item1b"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Item1a" type="xs:string "/>
<xs:element name="Item1b" type="xs:string "/>
<xs:element name="Item2" type="xs:string "/>
<xs:element name="Item3" type="xs:string "/>
<xs:element name="MyData">
<xs:complexType >
<xs:sequence maxOccurs="unbo unded">
<xs:element name="DataItem"
type="DataItemT ype"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

So far, so good. You see the xs:choice item, that consists of either the
complex type ItemType, or a sequence of Item2 and Item3 (both simple
strings).

So.. these are both valid xml files:

<MyData xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespace SchemaLocation= "C:\mydata.xsd" >
<DataItem>
<Item1>
<Item1a>Value 1</Item1a>
<Item1b>Value 2</Item1b>
</Item1>
</DataItem>
</MyData>
and...

<MyData xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespace SchemaLocation= "C:\mydata.xsd" >
<DataItem>
<Item2>Value1 </Item2>
<Item3>Value2 </Item3>
</DataItem>
</MyData>

Right. The XSD.Exe tool makes this of my XSD:

//----------------------------------------------------------------------
--------
// <autogenerate d>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be
lost if
// the code is regenerated.
// </autogenerated>
//----------------------------------------------------------------------
--------

//
// This source code was auto-generated by xsd, Version=1.1.432 2.573.
//
using System.Xml.Seri alization;
/// <remarks/>
[System.Xml.Seri alization.XmlRo otAttribute(Nam espace="",
IsNullable=fals e)]
public class MyData {

/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( "DataItem")]
public DataItemType[] DataItem;
}

/// <remarks/>
public class DataItemType {

/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( "Item2", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item3", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item1", typeof
(Item1Type))]
[System.Xml.Seri alization.XmlCh oiceIdentifierA ttribute
("ItemElementNa me")]
public object Item;

/// <remarks/>
[System.Xml.Seri alization.XmlIg noreAttribute()]
public ItemChoiceType ItemElementName ;
}

/// <remarks/>
public class Item1Type {

/// <remarks/>
public string Item1a;

/// <remarks/>
public string Item1b;
}

/// <remarks/>
[System.Xml.Seri alization.XmlTy peAttribute(Inc ludeInSchema=fa lse)]
public enum ItemChoiceType {

/// <remarks/>
Item2,

/// <remarks/>
Item3,

/// <remarks/>
Item1,
}

Now, this doesn't seem right. I can now have in my XML file an Item1,
and Item2 OR an Item3, instead of an Item1 OR (an Item2 AND an Item3).

Can anybody help me out here? What is it that I am doing wrong?

Thanks in advance
Dennis Vroegop
Nov 11 '05 #1
4 8999
Hi Dennis,

I have sent your question on to one of our XML experts. I will reply with
his answer ASAP.

Brett Keown
Microsoft Support
br*****@online. microsoft.com

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 11 '05 #2
Dennis,

Looks like the XSD tool doesn't account for a sequence definition inside a
choice model group ...

This may not be a bug as much as it is a design limitation of the tool and
the XmlSerializer. There is no way to express that Item2 and Item3 belong
together, since they appear at the same level of the document.

What you can do is to make the two fields that identify the choice array
types:

/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( "Item2", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item3", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item1", typeof
(Item1Type))]
[System.Xml.Seri alization.XmlCh oiceIdentifierA ttribute
("ItemElementNa me")]
public object[] Item;

/// <remarks/>
[System.Xml.Seri alization.XmlIg noreAttribute()]
public ItemChoiceType[] ItemElementName ;

Now you can at least assign Item2 and Item3 values.

Unfortunately. I can't think of a better way to work around this. You still
need to make sure you have schema valid document by validating the
serialized document to against your schema, or make sure in code that you
never assign invalid combinations Item1, Item2 and Item2 to the Item[]

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Dennis Vroegop" <dv******@detri o.nl> wrote in message
news:MP******** *************** *@news.xs4all.n l...
Hi there!

I have the following XSD (Well, this is not the real one, but the
concept is the same....)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefa ult="qualified" >
<xs:complexTy pe name="DataItemT ype">
<xs:choice>
<xs:element name="Item1" type="Item1Type "/>
<xs:sequence>
<xs:element ref="Item2"/>
<xs:element ref="Item3"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexTy pe name="Item1Type ">
<xs:sequence>
<xs:element ref="Item1a"/>
<xs:element ref="Item1b"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Item1a" type="xs:string "/>
<xs:element name="Item1b" type="xs:string "/>
<xs:element name="Item2" type="xs:string "/>
<xs:element name="Item3" type="xs:string "/>
<xs:element name="MyData">
<xs:complexType >
<xs:sequence maxOccurs="unbo unded">
<xs:element name="DataItem"
type="DataItemT ype"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

So far, so good. You see the xs:choice item, that consists of either the
complex type ItemType, or a sequence of Item2 and Item3 (both simple
strings).

So.. these are both valid xml files:

<MyData xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespace SchemaLocation= "C:\mydata.xsd" >
<DataItem>
<Item1>
<Item1a>Value 1</Item1a>
<Item1b>Value 2</Item1b>
</Item1>
</DataItem>
</MyData>
and...

<MyData xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespace SchemaLocation= "C:\mydata.xsd" >
<DataItem>
<Item2>Value1 </Item2>
<Item3>Value2 </Item3>
</DataItem>
</MyData>

Right. The XSD.Exe tool makes this of my XSD:

//----------------------------------------------------------------------
--------
// <autogenerate d>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be
lost if
// the code is regenerated.
// </autogenerated>
//----------------------------------------------------------------------
--------

//
// This source code was auto-generated by xsd, Version=1.1.432 2.573.
//
using System.Xml.Seri alization;
/// <remarks/>
[System.Xml.Seri alization.XmlRo otAttribute(Nam espace="",
IsNullable=fals e)]
public class MyData {

/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( "DataItem")]
public DataItemType[] DataItem;
}

/// <remarks/>
public class DataItemType {

/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( "Item2", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item3", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item1", typeof
(Item1Type))]
[System.Xml.Seri alization.XmlCh oiceIdentifierA ttribute
("ItemElementNa me")]
public object Item;

/// <remarks/>
[System.Xml.Seri alization.XmlIg noreAttribute()]
public ItemChoiceType ItemElementName ;
}

/// <remarks/>
public class Item1Type {

/// <remarks/>
public string Item1a;

/// <remarks/>
public string Item1b;
}

/// <remarks/>
[System.Xml.Seri alization.XmlTy peAttribute(Inc ludeInSchema=fa lse)]
public enum ItemChoiceType {

/// <remarks/>
Item2,

/// <remarks/>
Item3,

/// <remarks/>
Item1,
}

Now, this doesn't seem right. I can now have in my XML file an Item1,
and Item2 OR an Item3, instead of an Item1 OR (an Item2 AND an Item3).

Can anybody help me out here? What is it that I am doing wrong?

Thanks in advance
Dennis Vroegop

Nov 11 '05 #3
Hi Dennis,

There are known issues and limiations on the Xsd.exe tool. While there are
many ways to do things in Xsd, not all of those have been implmeneted in
the tool, and some only partially.

For the problem that you are running into, I would say there is nothing
wrong with the way you have written it.

The problem is this

For the following element, how would you code it for a class

<xs:choice>
<xs:element name="Item1" type="Item1Type "/>
<xs:sequence>
<xs:element ref="Item2"/>
<xs:element ref="Item3"/>
</xs:sequence>
</xs:choice>
In this case, we have to create it as such.

public object Item.
Then how do you tell it is can be called (item1, or item2 and item3)?

Unfortunately, there isn't any easy way, and to make a class out of it,
would be incorrect.

So, it does its best to "fudge" it. and throws on the following

[System.Xml.Seri alization.XmlEl ementAttribute( "Item2", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item3", typeof
(string))]
[System.Xml.Seri alization.XmlEl ementAttribute( "Item1", typeof
(Item1Type))]

It is difficult to sometimes put into code what is possible in Xsd.

Bruce Taimana

Microsoft Developer Support XML WebData Group

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.

© 2001 Microsoft Corporation. All rights reserved.

Nov 11 '05 #4
In article <uL************ **@TK2MSFTNGP11 .phx.gbl>,
ch************* *******@austin. rr.com says...
Dennis,

Looks like the XSD tool doesn't account for a sequence definition inside a
choice model group ...

Thank you all for your help and your time. I can go on with the project
with the solutions you all proposed. I agree that the XSD as I presented
it isn't the right way to do it (Item2 and Item3 should have been in a
complextype, imho) but there is nothing I can do about that. I have to
deal with whatever they give me :-(

Anyway, I can continue now. Thanks!

Dennis Vroegop
Detrio Consultancy b.v.
Nov 11 '05 #5

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

Similar topics

198
7731
by: Sridhar R | last post by:
>From technical point of view, I could not understand the the reasoning behind using Java in major companies. Sure that Python, is used in some, but still Java is considered as a sure-job language. After being a python programmer for long time, I consider it painful to learn/use Java now (well, like many I will be forced to do that in my job). What makes such companies to choose Java over dynamic, productive languages like Python? ...
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.
3
1540
by: kosaraju.puneeth | last post by:
I want an element that must have either attribute A or B. <Thing1 A="aaa"/> <Thing1 B="bbb"/> I want to enforce this in my schema . If A and B were elements, I could use choice.
1
2914
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"/>
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...
1
1501
by: SL33PY | last post by:
Hi, I'm currently busy writing an xsd at one point in time i whish that my xml must look like: <object> <loc> <disk> <drive>c</drive> <path>temp\path</path> <files>*.*</files> </disk>
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?
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
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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,...
1
10142
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
9021
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...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5422
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.