views:

313

answers:

1

Is there a way to replace all existing values from XML with values from dictionary using Linq? I'm using c#.

XML example:

<root>
  <node1> <--without attribute
    <subNode1>someTextValue</subNode1>
  </node1>
  <node2 name="myNode2"> <--With attribute (IMPORTANT!!!)
    <subNod1>someOtherTextValue</subNode1>
  </node2>
</root>


Dictionary<string, Hashtable> dataFromXML;

dataFromXML Keys from the example are node1 (because here is note an attribute), myNode2 (because here is an attribute)...

dataFromXML Values are subNodes.

Now lets say we make some value changes in some subNode and we want to update the XML.

How could I do this instead of looping with foreach???

Hope my question is clear defined.

EDIT

What about XElement and the ReplaceWith funcation?

Best reagards.

A: 

I don't think that you can update the XDocument in the Linq query. Anyway will be easy to develop the foreach statement (trust me you will lose less time), and remember that the linq queries are traduced as loop statements so even if you only see one line of code, the loop actually exists...

Regards

MF