Reputation: 51
I am trying to load an attribute from a simple xml file. The xml contents look like:
<top>
<levels>
<number>4</number>
</levels>
</top>
And I want to get that "4" into a variable and I can't figure it out how to do it correctly.
I tried something like that:
XDocument xdoc = XDocument.Load("levelsXml.xml");
var levels = from query in xdoc.Descendants("levels")
select nrOfLevelsCompleted = Convert.ToInt32(query.Element("number"));
Upvotes: 1
Views: 175
Reputation: 108967
If your xml file always has a single <levels />
element, you can try
int levels = Convert.ToInt32(xdoc.Descendants("levels")
.Single().Element("number").Value);
Upvotes: 2