【问题标题】:How to deserialize property with the name which is keyword?如何反序列化名称为关键字的属性?
【发布时间】:2014-08-04 18:37:02
【问题描述】:

我有以下 XML 代码:

<sees direction="NE"/>
<sees direction="E"/>
<sees direction="SE">
    <object>diamond</object>
    <background>green</background>
</sees>
<sees direction="SW">
    <background>green</background>
</sees>

我对对象标签的反序列化有疑问。首先我手动编写属性,然后我使用 XSD 工具生成类并编辑 XMLElementAttribute。

System.Xml.Serialization.XmlElementAttribute("object", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string @object
{
    get
    {
        return this.objectField;
    }
    set
    {
        this.objectField = value;
    }
}

即使编译后我也没有找到填充该字段的方法,并且所有其余字段(例如背景)都可以正常工作。

我有什么遗漏的吗?

【问题讨论】:

    标签: c# xml serialization keyword


    【解决方案1】:

    你应该可以这样做:

    [XmlElement("object")]
    public string WhatEverNameYouLike {get; set;}
    

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2013-11-16
      • 1970-01-01
      相关资源
      最近更新 更多