我正在编写一个 Web 服务来反序列化 XML 文件,将它们映射到 POCO 并将它们保存到数据库中。我有一个定义模型的 XSD,并使用 xsd2code 生成模型。
这是一个例子。在我的 XSD 定义中,我具有以下属性:
<xs:element name="SeniorityDate" type="xs:date" nillable="true"
minOccurs="0">
<xs:annotation>
<xs:documentation>A nullable date.</xs:documentation>
</xs:annotation>
</xs:element>
xsd2code 生成以下内容(注意该属性的数据类型为 Nullable DateTime):
/// <summary>
/// A nullable date.
/// </summary>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "date", IsNullable = true)]
public System.Nullable<System.DateTime> SeniorityDate
{
get {return this._seniorityDate;}
set {this._seniorityDate = value;}
}
我进行了 PUT 调用,尝试了不同的方法为属性输入空值,但这些方法都不起作用:
<SeniorityDate></SeniorityDate>
<SeniorityDate/>
<SeniorityDate xsi:nil="true" />
尽管该属性被标记为可为空,但所有这些都返回 HttpRequestException。
如果我手动添加注释 [XmlElement(IsNullable=true)](即 XmlElement 而不是自动生成的 XmlElementAttribute),则将接受空值。
有没有办法让 XML 反序列化允许使用 [XmlElementAttribute(IsNullable=true)] 注释属性的空值?或者我可以以某种方式让 xsd2code 将 [XmlElement(IsNullable=true)] 放在属性上吗?