如何初始化用 System.Xml.Serialization.XmlElementAttribute(String, Type) 修饰的类型
我正在使用 WCF Web API 开发 RESTful Web 服务。另一方提供了 XSD 文件。我使用 xsd.exe 生成了 C# 类。但是,该架构包含一种我遇到问题的复杂类型:
<xs:complexType name="SearchableField">
<xs:choice>
<xs:element name="NumericValue" type="xs:float" minOccurs="1" maxOccurs="1"/>
<xs:element name="BooleanValue" type="xs:boolean" minOccurs="1" maxOccurs="1"/>
</xs:choice>
<xs:attribute name="type" type="SearchableFieldType" use="required"/>
</xs:complexType>
这是为复杂类型生成的代码:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class SearchableField {
private object itemField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("BooleanValue", typeof(bool), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("NumericValue", typeof(float), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
}
问题:如何初始化类型 Item 属性(这是一个普通对象),以便它可以根据模式进行序列化。
约束:架构已指定,因此我不能更改 XSD 文件。
以下是 XML 元素的预期外观示例:
<SearchableFields>
<SearchableField type="MEGAPIXELS">
<NumericValue>12</NumericValue>
</SearchableField>
<SearchableField type="WEATHER_RESISTANT">
<BooleanValue>true</BooleanValue>
</SearchableField>
<SearchableField type="WATER_RESISTANT">
<BooleanValue>false</BooleanValue>
</SearchableField>
</SearchableFields>
I'm working on a RESTful web-service with the WCF Web API. Another party provided the XSD files. I generated the C# classes with xsd.exe. However the schema contains one complex type that I'm having an issue with:
<xs:complexType name="SearchableField">
<xs:choice>
<xs:element name="NumericValue" type="xs:float" minOccurs="1" maxOccurs="1"/>
<xs:element name="BooleanValue" type="xs:boolean" minOccurs="1" maxOccurs="1"/>
</xs:choice>
<xs:attribute name="type" type="SearchableFieldType" use="required"/>
</xs:complexType>
This is the generated code for the complex type:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class SearchableField {
private object itemField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("BooleanValue", typeof(bool), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("NumericValue", typeof(float), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
}
Question: How to initialize type Item property - which is a plain object - so that it can be serialized according to the schema.
Constraint: The schema is already specified so I may not change the XSD file.
Here's an example of the XML element how it is expected to look like:
<SearchableFields>
<SearchableField type="MEGAPIXELS">
<NumericValue>12</NumericValue>
</SearchableField>
<SearchableField type="WEATHER_RESISTANT">
<BooleanValue>true</BooleanValue>
</SearchableField>
<SearchableField type="WATER_RESISTANT">
<BooleanValue>false</BooleanValue>
</SearchableField>
</SearchableFields>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(评论)
确实 -
NumericValue
在 xml / C# 中被声明为float
,并使用int
> 将引入无效的强制转换;然而,这有效:输出:
(comments)
Indeed -
NumericValue
is declared asfloat
in the xml / C#, and using anint
is going to introduce an invalid cast; this works, however:with output: