我正在尝试生成以下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; }
}
真的很感激任何帮助。感谢!!!
答案 0 :(得分: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; }
}
希望这有用,如果不抱歉浪费你的时间。
答案 1 :(得分: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; }
}