【问题标题】:How to add an attribute to a serialized XML node?如何将属性添加到序列化的 XML 节点?
【发布时间】:2009-05-19 12:21:01
【问题描述】:

假设我有一个这样的 C# 类:

[XmlRoot("floors")]
public class FloorCollection
{
    [XmlElement("floor")]
    public Floor[] Floors { get; set; }

}

我想序列化它并使用 WCF 发送到 REST API。但在发送之前,我需要以这种方式向楼层节点添加一个属性:<floors type="array">...</floors>

有什么想法吗?

【问题讨论】:

    标签: c# xml wcf xml-serialization


    【解决方案1】:

    只需将 type 属性添加到您的集合类中:

    [XmlRoot("floors")]
    public class FloorCollection
    {
        [XmlAttribute("type")]
        public string Type { get; set; }
        [XmlElement("floor")]
        public Floor[] Floors { get; set; }
    
    }
    

    【讨论】:

      【解决方案2】:

      如果您的意思是在不知道业务代码的情况下添加它,那么您可能必须在发送消息之前使用Message Inspectors 来修改消息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-06
        • 2021-10-24
        • 1970-01-01
        • 2017-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多