我有这部分和.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我被抛出一个例外说:
{“缺少选择‘项目’序列化所需的'TimeElementName‘成员”}
我无法解释为什么它会抛出这个错误,因为我似乎还记得,在我编辑类的其他部分之前,它是工作的,当我调试代码时,TimePeriod也会收到正确的值,直到我碰到以下一行: var序列化程序=新的XmlSerializer(this.GetType()),才会抛出异常;
是否有其他方法可以获得我想要的输出或解决此异常。
发布于 2016-11-09 12:29:07
我发现,由于某种原因,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 Itemhttps://stackoverflow.com/questions/40467905
复制相似问题