如何向序列化的 XML 节点添加属性? [英] How to add an attribute to a serialized XML node?

查看:34
本文介绍了如何向序列化的 XML 节点添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Assume I have a C# class like this:

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

}

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

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>

有什么想法吗?

推荐答案

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

Just add the type attribute into your collection class:

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

}

这篇关于如何向序列化的 XML 节点添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆