I need to read an XML file to a dictionary.
I read few guides and I only got confused from weird words that I don't understand (such as nodes, XML validation etc.). So, could you please walk me through?
I have an XML file which is written in this format:
<database>
<def number="1" name="one"/>
<def number="2" name="two"/>
</database>
As mentioned, I want to store it in a dictionary. How would I go about that?
var data = XElement.Parse(xml)
.Elements("def")
.ToDictionary(
el => (int)el.Attribute("number"),
el => (string)el.Attribute("name")
);
This:
XElement
(starting at <database>
)<def ...>
elements@number
as the key (interpreting as an int
), and @name
as the value (as a string)data
, which is implicitly typed as Dictionary<int,string>