簡體   English   中英

C#序列化:將屬性添加到節點列表

[英]c# serialization: add attribute to a node list

我正在嘗試生成以下xml,而唯一讓我退縮的是在cachestore / cachegroups上添加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類

        [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; }

}

希望這可以幫助您,如果不后悔會浪費您的時間。

您需要另一個類,然后從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; }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM