LinQ to XML; query descendants using parent node value -
hi have following xml structure:
<root> <persons> <personlist category="employee"> <person name="john" id="5" /> <person name="mary" id="10" /> </personlist> </persons> </root>
i looking use linqtoxml , in order list of available person can write query:
var persons = p in mydoc.descendants("person") select p;
now, have in order person where category in personlist element = specific value? can't use parent because need specify personlist element structure of xml may different 1 not element name. possible?
it sounds you're looking for
var people = mydoc.descendants("personlist") .where(p => p.attribute("category").value == something) .descendants("person");
if want category of specific <person>
element, can write
var category = elem.ancestorsandself("personlist") .first().attribute("category").value;
Comments
Post a Comment