c# - Filter descendants parent [or at least higher level than results] -
still linq newbie here, , having issues clause. i'm trying return found in printer tags, below element list type="lff".
if try output descendant elements no clause, (from both <list>
elements). when try add various versions of clause, nothing back. i'm not putting condition in correctly.
(i need element object, can check name , value. in example below, outputting value now).
can advise?
here xml:
<?xml version="1.0"?> <printerlist> <list type="aff"> <printserver>print-server1</printserver> <printserver>print-server2</printserver> <printserver>print-server3</printserver> <additionalprinters> <printer> <fullname>\\servera\bbb</fullname> </printer> </additionalprinters> </list> <list type="lff"> <printserver>print-sever4</printserver> <additionalprinters> <printer> <fullname>\\serverb\bbb</fullname> </printer> <printer> <fullname>\\serverc\aaa</fullname> </printer> </additionalprinters> </list> </printerlist>
and here code try , list:
var qq = c in xml.descendants("additionalprinters").descendants("printer") //where (string) c.parent.attribute("type") == "lff" //uncommenting above line means nothing returned. select c; foreach (xelement q in qq) { console.writeline("test output: {0}", q.value ); }
output is:
test output: \\servera\bbb test output: \\serverb\bbb test output: \\serverc\aaa
i looking final 2 outputs returned, in particular case.
the parent of printer
additionalprinters
, doesn't have type
property, need use .parent
twice list
element.
from c in xml.descendants("additionalprinters").descendants("printer") (string) c.parent.parent.attribute("type") == "lff" select c
or can following
xml.descendants("list") .where(c => (string) c.attribute("type") == "lff") .selectmany(x => x.element("additionalprinters").descendants("printer"))
Comments
Post a Comment