0

I am programming in C# and trying to make a program to write XML with machine settings.

At the moment I use a listed dictionary, but in need to add extra categories.

Situation at the moment:

productlist = new List<Dictionary<string, string>>();

list with different products, dictionary with settings and their values.

But now with categories I need something like:

productlist = new List<Dictionary<string, Dictionary<string, string>>>();

list with different products, dictionary with category and dictionary with settings and their values.

Is this the easiest way to do this? Or can I better use classes (not very experienced with them..)

Edit:

Something like this
Product1:
-Category1
--SettingA with value;
--SettingB with value;
-Category2
--SettingC with value
--SettingD with value

Product2:
-Category1
--SettingA with value;
--SettingB with value;
-Category2
--SettingC with value;
--SettingD with value;

etc.

Mausdb
  • 13
  • 5
  • Possible duplicate of [Tree data structure in C#](https://stackoverflow.com/questions/66893/tree-data-structure-in-c-sharp) – jAC Nov 21 '17 at 09:42
  • Thanks, didnt know how to do that (show code) – Mausdb Nov 21 '17 at 09:43
  • So if I understand correcly you got a List with an entry for each catogorie known in your application. And each catogorie has it's own products and stuff? – Arno Nov 21 '17 at 09:48
  • In that case yes, I would say that replacing with a List or with a Dictionary would be best. this will make the loaded data into your code accessible. or if I missread and it is based on Products and then Categorie go with Er Suman's solution :) – Arno Nov 21 '17 at 09:52
  • Can a product belong to more than one category? (i.e a movie can be a science-fiction comedy) – Zohar Peled Nov 21 '17 at 09:53
  • Take a look at this: https://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us – ChristianMurschall Nov 21 '17 at 09:56

2 Answers2

2

You should try 'class'.

Create model classes of Product and Category as,

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

}

then create list of objects and add data as,

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" } }
        },

    }
});

If you want to print all settings of a Product then use loop,

foreach (var item in lstProducts[0].Categories)
{
       foreach (var vals in item.Settings.Values)
       {
             Print(vals);
        }
 }
Er Suman G
  • 581
  • 5
  • 12
1

I recommend you to use classes instead of nested dictionarys. Create a class 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
}

and a class 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
}

Then you can create your products with a list of settings like this:

ProductSetting setting = new ProductSetting('setting abc','setting def');
List<ProductSetting> settingsList = new List<ProductSetting>();
settingsList.Add(setting);
Product product = new Product('productname','productcategory',settingsList);

After that you can create a list of Products as you did:

List<Product> productsList = new List<Product>();
productsList.Add(product);
dns_nx
  • 3,651
  • 4
  • 37
  • 66