如何将属性添加到序列化XML节点?

时间:2022-05-22 19:36:11

Assume I have a C# class like this:

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

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

}

And I want to serialize it and send to a REST API using WCF. But before sending I need adding an attribute to the floors node in this way: <floors type="array">...</floors>

我想序列化它并使用WCF发送到REST API。但在发送之前,我需要以这种方式向楼层节点添加属性: ...

Any idea?

2 个解决方案

#1


Just add the type attribute into your collection class:

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

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

}

#2


If you mean adding that without the business code knowing about it, then you'll probably have to use Message Inspectors to modify the message before it is sent.

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

#1


Just add the type attribute into your collection class:

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

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

}

#2


If you mean adding that without the business code knowing about it, then you'll probably have to use Message Inspectors to modify the message before it is sent.

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