2016-02-02 42 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