I have this part of and .xsd:
<xs:element name="TimePeriod" nillable="false">
<xs:complexType>
<xs:choice>
<xs:element name="StartTime" type="xs:dateTime" nillable="false"/>
<xs:element name="StopTime" type="xs:dateTime" nillable="false"/>
</xs:choice>
</xs:complexType>
</xs:element>
With this code that I got from xsd2code:
public partial class ActivityTYPETimePeriod
{
private System.DateTime itemField;
private ItemChoiceType itemElementNameField;
public System.DateTime Item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName
{
get
{
return this.itemElementNameField;
}
set
{
this.itemElementNameField = value;
}
}
}
public enum ItemChoiceType
{
/// <remarks/>
StartTime,
/// <remarks/>
StopTime,
}
This gives me this output:
<TimePeriod>
<Item>2016-11-07T09:50:41.27</Item>
</TimePeriod>
but I would like to be like this if StartTime is the enum selection:
<TimePeriod>
<StartTime>2016-11-07T09:50:41.27</StartTime>
</TimePeriod>
But when I use this decoration (also from xsd2code):
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public System.DateTime Item
I get thrown an exception saying:
{"Missing 'TimeElementName' member needed for serialization of choice 'Item'."}
I can't put my finger on why it throws this error as I seem to remember it was working before I edited some other parts of my class and when I debug the code the TimePeriod
recieves the correct values as well and the exception is not thrown until I hit the this line:
var serializer = new XmlSerializer(this.GetType());
Is there another way to get my desired output or solve this exception.
I found that the lacking part that xsd2code did not generate for some reason was these two lines of code that also was required for it to work:
[System.Xml.Serialization.XmlElementAttribute("EndTime", typeof(System.DateTime))]
[System.Xml.Serialization.XmlElementAttribute("StartTime", typeof(System.DateTime))]
So the resulting decoration on Item
is like this:
[System.Xml.Serialization.XmlElementAttribute("Sluttidpunkt", typeof(System.DateTime))]
[System.Xml.Serialization.XmlElementAttribute("Starttidpunkt", typeof(System.DateTime))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public System.DateTime Item