生成的 xsd2code 从 XmlChoiceIdentifierAttribute 抛出错误

xsd2code generated throws error from XmlChoiceIdentifierAttribute

我有这部分和.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>

使用我从 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,
}

这给了我这个输出:

<TimePeriod>
    <Item>2016-11-07T09:50:41.27</Item>
</TimePeriod>

但如果 StartTime 是枚举选择,我想像这样:

<TimePeriod>
    <StartTime>2016-11-07T09:50:41.27</StartTime>
</TimePeriod>

但是当我使用这个装饰时(也来自xsd2code):

[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public System.DateTime Item

我抛出异常说:

{"Missing 'TimeElementName' member needed for serialization of choice 'Item'."}

我不明白为什么它会抛出这个错误,因为我似乎记得它在我编辑我的 class 的其他部分之前以及在我调试代码时 TimePeriod 也收到正确的值,并且在我点击此行之前不会抛出异常: var serializer = new XmlSerializer(this.GetType());

是否有其他方法可以获得我想要的输出或解决此异常。

我发现 xsd2code 由于某种原因没有生成的缺失部分是这两行代码,这也是它工作所必需的:

[System.Xml.Serialization.XmlElementAttribute("EndTime", typeof(System.DateTime))]
[System.Xml.Serialization.XmlElementAttribute("StartTime", typeof(System.DateTime))]

所以Item上的装饰效果是这样的:

[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