我正在使用C#进行编程,并尝试使用机器设置编写程序来编写XML。
目前我使用列出的字典,但需要添加额外的类别。
目前的情况:
productlist = new List<Dictionary<string, string>>();
列出不同的产品,带设置的字典及其值。
但现在我需要类别:
productlist = new List<Dictionary<string, Dictionary<string, string>>>();
包含不同产品的列表,包含类别的字典和包含设置及其值的字典。
这是最简单的方法吗?或者我可以更好地使用课程(对他们不是很有经验......)
编辑:
像这样的东西
产品1:
-Category1
- 用值设置A;
- 用值设置B;
-Category2
- 使用值设置C
- 设置值为值
产品2:
-Category1
- 用值设置A;
- 用值设置B;
-Category2
- 用值设置C;
- 用值设置D;
等
答案 0 :(得分:2)
你应该尝试'上课'。
创建产品和类别的模型类,
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public List<Category> Categories { get; set; }
}
public class Category
{
public string Name { get; set; }
public Dictionary<string, string> Settings { get; set; }
}
然后创建对象列表并将数据添加为
List<Product> lstProducts = new List<Product>();
lstProducts.Add(new Product()
{
ProductID = 1001,
Name = "Product1",
Categories = new List<Category>()
{
new Category(){
Name ="Category1",
Settings =new Dictionary<string, string>{
{ "SettingA", "val1" },
{ "SettingB", "val2" } }
},
new Category(){
Name ="Category2",
Settings =new Dictionary<string, string>{
{ "SettingA", "val1" },
{ "SettingB", "val2" } }
},
}
});
如果要打印产品的所有设置,请使用循环
foreach (var item in lstProducts[0].Categories)
{
foreach (var vals in item.Settings.Values)
{
Print(vals);
}
}
答案 1 :(得分:1)
我建议你使用类而不是嵌套的dictionarys。
创建课程ProductSetting
public class ProductSetting
{
#region Variables
private string _Setting1 = string.Empty;
private string _Setting2 = string.Empty;
//...
#endregion Variables
#region Properties
public string Setting1
{
get
{
return this._Setting1;
}
set
{
this._Setting1 = value;
}
}
public string Setting2
{
get
{
return this._Setting2;
}
set
{
this._Setting2 = value;
}
}
#endregion Properties
#region Constructors
public ProductSetting()
{
}
public ProductSetting(string setting1, string setting2)
{
this.Setting1 = setting1;
this.Setting2 = setting2;
}
#endregion Constructors
}
和一个班级Product
public class Product
{
#region Variables
private string _Name = string.Empty;
private string _Category = string.Empty;
private List<ProductSetting> _Settings = new List<ProductSetting>();
#endregion Variables
#region Properties
public string Name
{
get
{
return this._Name;
}
set
{
this._Name = value;
}
}
public string Category
{
get
{
return this._Category;
}
set
{
this._Category = value;
}
}
public List<ProductSetting> Settings
{
get
{
return this._Settings;
}
set
{
this._Settings = value;
}
}
#endregion Properties
#region Constructors
public Product()
{
}
public Product(string name, string category, List<ProductSetting> settings)
{
this.Name = name;
this.Category = category;
this.Settings = settings;
}
#endregion Constructors
}
然后,您可以使用以下设置列表创建产品:
ProductSetting setting = new ProductSetting('setting abc','setting def');
List<ProductSetting> settingsList = new List<ProductSetting>();
settingsList.Add(setting);
Product product = new Product('productname','productcategory',settingsList);
之后,您可以像以前一样创建产品列表:
List<Product> productsList = new List<Product>();
productsList.Add(product);