c# - Select Parent XML(Entire Hierarchy) Elements based on Child element values LINQ -
i have following xml , query through id,how parent hierarchy
<child>     <child1 id="1">         <child2 id="2">             <child3 id="3">                 <child4 id="4">                     <child5 id="5"/>                     <child6 id="6"/>                 </child4>             </child3>         </child2>     </child1> </child>   in if query(id = 4) , find out parent elements using linq in particular element how following output hierarchy.
<child>     <child1 id="1">         <child2 id="2">             <child3 id="3">                 <child4 id="4"/>             </child3>         </child2>     </child1> </child>   thanks in advance.
assume want 1 node parent tree:
string xml = @"<child>                 <child1 id="1">                   <child2 id="2">                     <child3 id="3">                       <child4 id="4">                         <child5 id="5"/>                         <child6 id="6"/>                       </child4>                   </child3>                  </child2>                </child1>              </child>";  textreader tr = new stringreader(xml); xdocument doc = xdocument.load(tr);  ienumerable<xelement> mylist =     el in doc.descendants()     (string)el.attribute("id") == "4" // here whatever want     select el;   // select hero element in way  xelement hero = mylist.firstordefault();  foreach (xelement ancestor in hero.ancestors()) {     console.writeline(ancestor.name); // rebuild tree in separate document, print ;) }   to search every element of tree iterate retrieve node select query without clause , call foreach every element.
Comments
Post a Comment