How to add keypair values into the dictionary directly from the xml file. On uploading the keypair values of an xml file should be added to the dictionary... And from that I should read those keypair values
Asked
Active
Viewed 103 times
-4
-
http://stackoverflow.com/questions/18261921/how-to-add-an-xml-file-and-read-key-value-pair-into-a-dictionary might help – MusicLovingIndianGirl Oct 07 '15 at 05:17
-
While jhmt's answer is probably sufficient, it would be very helpful to see an example of your XML structure so we can provide you with more appropriate answers, rather than relying on guesswork. – Eraph Oct 07 '15 at 05:48
1 Answers
3
If your xml file's scheme is like this,
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Key1>Val1</Key1>
<Key2>Val2</Key2>
<Key3>Val3</Key3>
</Root>
I think this would be work well.
var xdoc = XDocument.Load(pathToXmlFile);
var dictionary = xdoc.Root.Elements().ToDictionary(k => k.Name, v => v.Value);

jhmt
- 1,401
- 1
- 11
- 15
-
It's worth noting that the type of the dictionary you're producing is `Dictionary
`. – Enigmativity Oct 07 '15 at 06:18 -
It shoudn't be done like this. Here we need to do the de-serialization, we need to create the class of value pair type. – SHIVANG RANA Oct 07 '15 at 06:52