c# 序列化:将属性添加到节点列表

c# serialization: add attribute to a node list

我正在尝试生成以下 xml,唯一阻碍我的是在 [=20= 上添加 group-selector-type 属性].我只是不确定在哪里添加 属性 以及它需要如何装饰。

<cachestore >
            <cachegroups group-selector-type="">
                 <cachegroup  name="group 1" />
                 <cachegroup  name="group 1" />
            </cachegroups>    
</cachestore>

这是我的 C# 类:

[XmlRoot("cachestore")]
public class CacheStoreConfig
{

    [XmlAttribute("type")]
    public String TypeName { get; set; }

    [XmlArray("cachegroups")]
    public List<CacheGroupConfig> CacheGroups { get; set; }

}

[XmlType("cachegroup")]
    public class CacheGroupConfig
    {
        [XmlAttribute("name")]
        public String Name { get; set; }
        [XmlAttribute("item-expiration")]
        public int ItemExpiration { get; set; }
        [XmlAttribute("max-size")]
        public string MaxSize { get; set; }
    }

非常感谢任何帮助。谢谢!!!

将此添加到您的 CacheGroupConfig Class

        [XmlAttribute("group-selector-type")]
    public string group_selector_type = "Whatever";

是的,很抱歉,我对问题的了解不够。

我能够让我的 XML 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<cachestore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CacheGroups group-selector-type="bubba">
    <groups>
      <CacheGroupConfig name="Name1" item-expiration="10" max-size="10 tons" />
      <CacheGroupConfig name="Name2" item-expiration="20" max-size="100 Light Years" />
    </groups>
  </CacheGroups>
</cachestore>

与以下 类

public class CacheGroups
{
    [XmlAttribute("group-selector-type")]
    public string group_selector_type = "bubba";

    [XmlArray]
    public List<CacheGroupConfig> groups { get; set; }
}

public class CacheGroupConfig
{
    [XmlAttribute("name")]
    public string Name { get; set; }
    [XmlAttribute("item-expiration")]
    public int ItemExpiration { get; set; }
    [XmlAttribute("max-size")]
    public string MaxSize { get; set; }

    public CacheGroupConfig()
    {
        //empty
    }

    public CacheGroupConfig( string name, int itemExpiration, string maxSize)
    {
        Name = name;
        ItemExpiration = itemExpiration;
        MaxSize = maxSize;
    }
}

[XmlRoot("cachestore")]
public class CacheStoreConfig
{

    [XmlAttribute("type")]
    public string TypeName { get; set; }

    public CacheGroups CacheGroups { get; set; }

}

抱歉浪费您的时间,希望这对您有所帮助。

您需要另一个 class 并将 XmlArray 更改为 XmlElement。该数组添加了您不需要的另一层标签。

    [XmlRoot("cachestore")]
    public class CacheStoreConfig
    {

        [XmlAttribute("type")]
        public String TypeName { get; set; }

        [XmlElement("cachegroups"]
        public CacheGroups cacheGroups { get; set; }

    }

    [XmlType("cachegroups")]
    public class CacheGroups
    {
        [XmlElement("cachegroups")]
        public List<CacheGroupConfig> CacheGroupConfig { get; set; }
        [XmlAttribute("group-selector-type")]
        public String group_selector_type { get; set; }
    }

    [XmlType("cachegroup")]
    public class CacheGroupConfig
    {
        [XmlAttribute("name")]
        public String Name { get; set; }
        [XmlAttribute("item-expiration")]
        public int ItemExpiration { get; set; }
        [XmlAttribute("max-size")]
        public string MaxSize { get; set; }
    }