Dilemma with XSD, Generics and C# Classes

I have following simple XSD file:

<xs:element name="Search" type="SearchObject"/> <xs:complexType name="SearchObject"> <xs:choice> <xs:element name="Simple" type="SimpleSearch"/> <xs:element name="Extended" type="ExtendedSearch"/> </xs:choice> </xs:complexType> <xs:complexType name="SimpleSearch"> <xs:sequence> <xs:element name="FirstName" type="xs:string"/> <xs:element name="LastName" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="ExtendedSearch"> <xs:sequence> <xs:element name="FirstName" type="xs:string"/> <xs:element name="LastName" type="xs:string"/> <xs:element name="Age" type="xs:int"/> <xs:element name="Address" type="xs:string"/> </xs:sequence> </xs:complexType>

I use Visual Studio Shell like this:

xsd XMLSchema.xsd /c

Basically /c stands for generating C# classes out of XMLSchema.xsd.

The classes then look something like this one:

[System.Xml.Serialization.XmlRootAttribute("Search", Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)] public partial class SearchObject { private object itemField; [System.Xml.Serialization.XmlElementAttribute("Extended", typeof(ExtendedSearch))] [System.Xml.Serialization.XmlElementAttribute("Simple", typeof(SimpleSearch))] public object Item { get { return this.itemField; } set { this.itemField = value; } } }

My first question is why is the property "Item" not called "Search" as I have set inside xsd file on that element?

My second question is why is property Item of type object? I have set a choice inside my xsd file and I would like the c# code to look more like this:

public partial class SearchObject<T> where T : SimpleSearch, where T : ExtendedSearch { public T Search { get ... set ... } }

I would like to have somehow an generic class that allows only the types which I have specified inside the choice block in xsd file which are in my case SimpleSearch and ExtendedSearch.

Is that even possible and if yes how do I get it right?


Choice in xsd means you could have one of the different object types declared. And because of that, the xsd.exe generates an object(always named Item) instead of a strong type. See: http://msdn.microsoft.com/en-us/library/sa6z5baz(v=vs.85).aspx. You have to check during run time what the object type is:

ExtendedSearch extendedSearch = null; SimpleSearch simpleSearch = null; if(Item is ExtendedSearch) extendedSearch = (ExtendedSearch)Item; else if(Item is SimpleSearch) simpleSearch = (SimpleSearch)Item;


  • Difference between devpi and pypi server
  • Find generic sub-lists within a list
  • Sum, Avg, Max, Min, Count of NULL values
  • Select an attribute with Xpath in a XML with namespace using Powershell
  • Spring custom user details service null pointer exception
  • error C2099: initializer is not a constant
  • Run multiple web applications in one Visual Studio development server?
  • Unable to deploy on Windows 10 Mobile
  • Meteor throws throwIfSelectorIsNotId exception
  • Django-ckeditor not displaying correctly in html
  • mfc - MessageBox with rich text
  • Is it possible to access raw iphone audio output?
  • Wpf RichTextBox wrapping problems
  • SessionInformation expiration doesn't lead to user log out from system
  • How to get a Windows Forms panel as a bitmap with C#?
  • Filtering SPARQL results by day and month
  • how to post with curl to REST/JSON service?
  • Max of several columns
  • How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configur
  • What dll is needed for Windows.Devices.Geolocation?
  • not able to create VC++ project, with VS11
  • Visual studio 2015 keystroke with mouse button
  • XSD with multi occurrences unordered
  • TFS - how do I sum child task hours to parent
  • Deploying a CodeRush plugin from the Community Site
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • How to pass solution folder as parameter in command line arguments (for debug)?
  • How can I get the full list of running processes on a Mac from a python app
  • How to convert SOAP response with xsi values to json in WSO2esb
  • ASP.NET MVC 2 Preview 2 - display directory list rather than home/index
  • Is playing sound in Javascript performance heavy?
  • Jetty 9 HashLoginService
  • Spark fat jar to run multiple versions on YARN
  • Cannot resolve symbol 'MyApi'
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal