XmlAttributeAttribute 和 XmlElementAttribute

发布于 2024-12-09 13:47:28 字数 410 浏览 2 评论 0原文

我想在属性上声明 XmlAttributeAttribute 和 XmlElementAttribute,以便 xml 正确反序列化,无论该属性是定义为 xml 元素还是 xml 属性。

例如 给定

public class X
{
    [XmlElement()]
    [XmlAttribute()]        
    public string Prop
    {
        get;
        set;
    }
}

以下任一条件,可以正确反序列化:

<X>
    <Prop>XXX</Prop>
</X>

<X Prop="XXX"/>

这可能吗?

I'd like to declare both XmlAttributeAttribute and XmlElementAttribute on a property so that the xml deserializes correctly regardless of whether the property is defined as an xml element or as an xml attribute.

e.g.
given

public class X
{
    [XmlElement()]
    [XmlAttribute()]        
    public string Prop
    {
        get;
        set;
    }
}

either of the following deserialize correctly:

<X>
    <Prop>XXX</Prop>
</X>

<X Prop="XXX"/>

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迷离° 2024-12-16 13:47:28

您可以像这样引入转发属性

public class X
{
    [XmlElement()]
    public string Prop
    {
        get;
        set;
    }

    [XmlAttribute("Prop")]
     public string Prop1
    {
        get { return Prop; }
        set 
        {
             if (!string.IsNullOrEmpty(value))
             {
                  Prop = value;
             }
        }
    }
}

You can introduce a forwarding property like this

public class X
{
    [XmlElement()]
    public string Prop
    {
        get;
        set;
    }

    [XmlAttribute("Prop")]
     public string Prop1
    {
        get { return Prop; }
        set 
        {
             if (!string.IsNullOrEmpty(value))
             {
                  Prop = value;
             }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文