469,607 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

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"
elementFormDefault="qualified">
<xs:complexType name="DataItemType">
<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:complexType 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="unbounded">
<xs:element name="DataItem"
type="DataItemType"/>
</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:noNamespaceSchemaLocation="C:\mydata.xsd">
<DataItem>
<Item1>
<Item1a>Value1</Item1a>
<Item1b>Value2</Item1b>
</Item1>
</DataItem>
</MyData>
and...

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

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

//----------------------------------------------------------------------
--------
// <autogenerated>
// 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.4322.573.
//
using System.Xml.Serialization;
/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)]
public class MyData {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Data Item")]
public DataItemType[] DataItem;
}

/// <remarks/>
public class DataItemType {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", typeof
(Item1Type))]
[System.Xml.Serialization.XmlChoiceIdentifierAttrib ute
("ItemElementName")]
public object Item;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName;
}

/// <remarks/>
public class Item1Type {

/// <remarks/>
public string Item1a;

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

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(IncludeI nSchema=false)]
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 8731
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.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", typeof
(Item1Type))]
[System.Xml.Serialization.XmlChoiceIdentifierAttrib ute
("ItemElementName")]
public object[] Item;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
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******@detrio.nl> wrote in message
news:MP************************@news.xs4all.nl...
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"
elementFormDefault="qualified">
<xs:complexType name="DataItemType">
<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:complexType 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="unbounded">
<xs:element name="DataItem"
type="DataItemType"/>
</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:noNamespaceSchemaLocation="C:\mydata.xsd">
<DataItem>
<Item1>
<Item1a>Value1</Item1a>
<Item1b>Value2</Item1b>
</Item1>
</DataItem>
</MyData>
and...

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

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

//----------------------------------------------------------------------
--------
// <autogenerated>
// 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.4322.573.
//
using System.Xml.Serialization;
/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)]
public class MyData {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Data Item")]
public DataItemType[] DataItem;
}

/// <remarks/>
public class DataItemType {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", typeof
(Item1Type))]
[System.Xml.Serialization.XmlChoiceIdentifierAttrib ute
("ItemElementName")]
public object Item;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName;
}

/// <remarks/>
public class Item1Type {

/// <remarks/>
public string Item1a;

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

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(IncludeI nSchema=false)]
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.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

198 posts views Thread by Sridhar R | last post: by
1 post views Thread by Oleg Ogurok | last post: by
4 posts views Thread by Sergey Poberezovskiy | last post: by
2 posts views Thread by hooomee | last post: by
reply views Thread by Peter Larsen | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.