如何初始化用 System.Xml.Serialization.XmlElementAttribute(String, Type) 修饰的类型

发布于 2024-12-12 03:42:52 字数 2167 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-12-19 03:42:52

(评论)

当我这样尝试时,它无法序列化:

var field = new SearchableField { type = "monitor_size", Item = 5 };

确实 - NumericValue 在 xml / C# 中被声明为 float,并使用 int > 将引入无效的强制转换;然而,这有效:

var field = new SearchableField { type = "monitor_size", Item = 5F };

输出:

<SearchableField type="monitor_size">
  <NumericValue>5</NumericValue>
</SearchableField>

(comments)

When I try it like this, it can not be serialized:

var field = new SearchableField { type = "monitor_size", Item = 5 };

Indeed - NumericValue is declared as float in the xml / C#, and using an int is going to introduce an invalid cast; this works, however:

var field = new SearchableField { type = "monitor_size", Item = 5F };

with output:

<SearchableField type="monitor_size">
  <NumericValue>5</NumericValue>
</SearchableField>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文