Adding Object Properties and object as Keys in Dictionary RRS feed

  • Question

  •        I am trying to read elements from xml file. I would like to add all the elements in xml file in a config file and then add them as keys in dictionary. As a first step, i just hard coded the keys. Eventually i will read the keys from config file. The keys will be the all the elements from large xml files. I need to write all the dictionary values to the db.  I am trying to return the last value in dictionary i.e. orders object to write to the db. The last dictionary value corresponding to the key "orders" is not updating with the properties like order number, orderdate and createdby. These are null.  Although the first three are updated with the correct values. orders.OrderNumber has value. orders.OrderDate has value, orders.CreatedBy also has value. But orders in ordersDic.Add("orders", orders); has no values for the these properties. The ordernumber, orderdate, createdby are null in the orders object.

    Is that means the dictionary is not storing the shallow copy of my object ? How can i change this so that when i update any of the property values by using the keys and updating the corresponding dictionary  values it updates the property value in orders object(the last dictionary value)

    For example if i update the order date :

    ordersDic["order-date"] = el.Value;

    it should also update the orders object(last dictionary element) OrderDate property. So that i can access this value simply by doing :orders.OrderDate

    XElement el = XElement.Load(subtree)
     Orders orders = new Orders();              
                    ordersDic.Add("original-order-no", orders.OrderNumber);
                    ordersDic.Add("order-date", orders.OrderDate);
                    ordersDic.Add("created-by", orders.CreatedBy);
                    ordersDic.Add("orders", orders);
                  ordersDic[el.Name.LocalName] = el.Value;


    • Edited by wpf_xam1 Friday, August 16, 2019 6:07 PM
    Friday, August 16, 2019 3:20 PM

All replies

  • I think you're trying to misuse the dictionary and it isn't going to work. I don't understand at all what you're talking about when you say you have some object and a last object and something else about keys in the dictionary. Posting a snippet of the XML you're reading, how that looks in the dictionary, how first/last/whatever has anything to do with that, how you defined the dictionary and what object(s) you're talking about would be useful.

    In general you'll use a dictionary when you want to be able to store named (or keyed) values together. If the data stored in each key isn't the same then a dictionary is not the correct solution. In your code sample you posted I can see you're adding what should probably be ints, dates and strings into a dictionary but I suspect that they are all strings because you're reading XML. You then insert into the dictionary an Orders object. That means you would have had to create your dictionary as storing objects and immediately that tells me you're doing it wrong. A dictionary should contain one and only one type (or derived). If it contains a mix then you're using a dictionary incorrectly (in most cases).

    I don't understand anything about your XML and why you feel it necessary to store each individual property of an order in the dictionary. Perhaps you can clarify that purpose. Personally it sounds like your XML is a list of orders and therefore you should simply be reading your XML file, creating an "order" for each high level element, setting the properties of the order based upon the child ements and storing that in a list. 

    The last thing that doesn't make sense to me given your example code is that you are using the type Orders but it appears to be a single order. You then seem to be inserting the property values of that order into the dictionary (presumably after you've read them from the XML) but a dictionary requires keys to be unique so if you read multiple "orders" then the last order would be the one that gets stored into the dictionary. Each of the previous orders would have been stomped over. In that case there was no benefit reading all the XML, just use XPath to get to the last one.

    Michael Taylor

    Friday, August 16, 2019 8:52 PM
  • Hi wpf_xam1, 

    Thank you for posting here.

    Based on your code, we cannot reproduce your problem.

    Could you provide the whole related code about what you have done and some information about the XML? It will help us to make a test and reproduce your problem.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, August 19, 2019 8:52 AM