2016-02-02 75 views
0

我正在嘗試生成以下xml,唯一讓我困擾的事情是在cachestore/cachegroups上添加組選擇器類型屬性。我只是不確定在哪裏添加該房產以及需要如何裝修。c#序列化:將屬性添加到節點列表

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

非常感謝所有幫助。謝謝!!!

回答

1

您需要另一個類並將其從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; } 
    } 
1

添加到您的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; } 

} 

希望這會幫助,如果不後悔浪費你的時間。

+0

這會有幫助嗎? cachestore/cachegroups是CacheGroupConfigs的列表 – webber