Reputation: 33
<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
<result>
<rowset name="typeids" key="typeID" columns="typeName,TypeID">
<row typeName="Construction Blocks" typeID="3828" />
</rowset>
</result>
</eveapi>
Currently I am trying to get the value of the typeID
attribute from this xml using this code:
var result = from el in doc.Elements("row")
where (string)el.Attribute("typeName") == "Construction Blocks"
select el.Attribute("typeID").Value;
foreach (string el in result)
{
typeID.Add(Convert.ToInt32(el));
}
However the foreach
statement never fires. What am I doing wrong here?
Edit: Sorry, I put in the wrong xml. Correct xml is now there
Upvotes: 1
Views: 45
Reputation: 37299
First of all you should use .Descendants()
and not Elements
:
var result = (from el in doc.Descendants("row")
where (string)el.Attribute("typeName") == "Construction Blocks"
select Convert.ToInt32(el.Attribute("typeID").Value)).ToList();
// See that you can just perform the `Convert` in the `select` instead of having a
// `foreach`. If you need to put the results into an initialized list then you can remove
// the `ToList()` and just have an `AddRange` on your list to the `result`
Upvotes: 1
Reputation: 222522
In your xml, there is no typeName
named Construction Blocks
, so the result will be obviously null.
So foreach loop does not have any collection.
Upvotes: 0