LINQ to XML (where clause)
-
Hi guys, I didn't know which forum to post this (LINQ or XML), so I'm posting it here. Anyway. I have a bit of problem with LINQ to XML here. This is my XML:
< ResourceIndex >
< Items Name = "List_A" >
< Item Value = "0" > A - option 1 < / Item >
< Item Value = "1" > A - option 2 < / Item >
< Item Value = "2" > A - option 3 < / Item >
< / Items >
< Items Name = "List_B" >
< Item Value = "0" > B - option 1 < / Item >
< Item Value = "2" > B - option 2 < / Item >
< / Items >
< / ResourceIndex >Well, what I wanna do is select the "Item" nodes with respective attributes, but from one specific Items Name=" ? ". Here is my code:
var q = from c in xmlDoc.Descendants("Items").Elements("Item")
select new
{
Text = c.Value,
isSelected = c.Attribute("Value").Value
};This returns a list of everything, but I just want a list of . I tried to insert the where clause but I didn't succeeded because the nodes that are being listed are the bottom level and it seems I can't use where from ancestors. Can anyone help me? :confused: Thanks a lot.
-
Hi guys, I didn't know which forum to post this (LINQ or XML), so I'm posting it here. Anyway. I have a bit of problem with LINQ to XML here. This is my XML:
< ResourceIndex >
< Items Name = "List_A" >
< Item Value = "0" > A - option 1 < / Item >
< Item Value = "1" > A - option 2 < / Item >
< Item Value = "2" > A - option 3 < / Item >
< / Items >
< Items Name = "List_B" >
< Item Value = "0" > B - option 1 < / Item >
< Item Value = "2" > B - option 2 < / Item >
< / Items >
< / ResourceIndex >Well, what I wanna do is select the "Item" nodes with respective attributes, but from one specific Items Name=" ? ". Here is my code:
var q = from c in xmlDoc.Descendants("Items").Elements("Item")
select new
{
Text = c.Value,
isSelected = c.Attribute("Value").Value
};This returns a list of everything, but I just want a list of . I tried to insert the where clause but I didn't succeeded because the nodes that are being listed are the bottom level and it seems I can't use where from ancestors. Can anyone help me? :confused: Thanks a lot.
Try this:
var q = from i in xmlDoc.Descendants("Items")
where i.Attribute("Name").Value == "List_A"
from c in i.Elements("Item")
select new
{
Text = c.Value,
isSelected = c.Attribute("Value").Value
};
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Try this:
var q = from i in xmlDoc.Descendants("Items")
where i.Attribute("Name").Value == "List_A"
from c in i.Elements("Item")
select new
{
Text = c.Value,
isSelected = c.Attribute("Value").Value
};
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you very much, Richard. I'd have no clue I'd have to use to 'from' clauses. Cheers. :-D