ValoChele

ValoChele

  • NA
  • 68
  • 21.6k

How to use Ancestors with Descendants in LINQ to XML?

Oct 16 2017 8:37 AM
How do I get the contents of a node <title> whose immediate ancestor is <sec> using linq 2 xml. Here is a sample xml file
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <article article-type="proceedings" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:oasis="http://www.niso.org/standards/z39-96/ns/oasis-exchange/table">  
  3. <front>  
  4. <title-group>  
  5. <title>SMALL angle scattering polarization biopsy: a comparative analysis of various skin diseases</title>  
  6. </title-group>  
  7. ....  
  8. ....  
  9. </front>  
  10. <body>  
  11. <boxed-text id="s1" position="anchor">  
  12. <sidebar-label>S1.</sidebar-label>  
  13. <sec id="sec1">  
  14. <title>EXPERIMENTAL TECHNIQUE</title>  
  15. <p>In the case of lupus the changes in the basal membrane adversely affect differentiation of the epidermis; this results in disorders of keratinization combined with progressive atrophy.</p>  
  16. </sec>  
  17. </boxed-text>  
  18. ....  
  19. ....  
  20. </body>  
I'm trying to get the value EXPERIMENTAL TECHNIQUE from the above example using both Descendants and Ancestors method of LINQ 2 XML. I've tried
  1. var item=from x in doc.Descendants("label")  
  2. where x.Ancestors("sec")  
  3. select x;  
but getting an error
 
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to 'bool'
</b> whose immediate ancestor is <b><sec> </b>using linq 2 xml.  Here is a sample xml file</div><div><div class="dp-highlighter"><div class="bar"></div><ol start="1" class="dp-xml"><li class="alt"><span><span class="tag"><?</span><span class="tag-name">xml</span><span> </span><span class="attribute">version</span><span>=</span><span class="attribute-value">"1.0"</span><span> </span><span class="attribute">encoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span class="tag">?></span><span>  </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">article</span><span> </span><span class="attribute">article-type</span><span>=</span><span class="attribute-value">"proceedings"</span><span> </span><span class="attribute">xmlns:xlink</span><span>=</span><span class="attribute-value">"http://www.w3.org/1999/xlink"</span><span> </span><span class="attribute">xmlns:mml</span><span>=</span><span class="attribute-value">"http://www.w3.org/1998/Math/MathML"</span><span> </span><span class="attribute">xmlns:oasis</span><span>=</span><span class="attribute-value">"http://www.niso.org/standards/z39-96/ns/oasis-exchange/table"</span><span class="tag">></span><span>  </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">front</span><span class="tag">></span><span>  </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">title-group</span><span class="tag">></span><span>  </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>SMALL angle scattering polarization biopsy: a comparative analysis of various skin diseases</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span>  </span></span></li><li class=""><span><span class="tag"></</span><span class="tag-name">title-group</span><span class="tag">></span><span>  </span></span></li><li class="alt"><span>....  </span></li><li class=""><span>....  </span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">front</span><span class="tag">></span><span>  </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>  </span></span></li><li class="alt"><span><span class="tag"><boxed-text id="s1" position="anchor"></span></span></li><li class="alt"><span><span class="tag"><sidebar-label>S1.</<span style="background-color: #ffffff;">sidebar-label</span>></span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">sec</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"sec1"</span><span class="tag">></span><span>  </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>EXPERIMENTAL TECHNIQUE</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span>  </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">p</span><span class="tag">></span><span>In the case of lupus the changes in the basal membrane adversely affect differentiation of the epidermis; this results in disorders of keratinization combined with progressive atrophy.</span><span class="tag"></</span><span class="tag-name">p</span><span class="tag">></span><span>  </span></span></li><li class=""><span><span class="tag"></</span><span class="tag-name">sec</span><span class="tag">></span></span></li><li class=""><span><span><span style="background-color: #ffffff;"></boxed-text></span>  </span></span></li><li class="alt"><span>....  </span></li><li class=""><span>....  </span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>  </span></span></li></ol></div>I'm trying to get the value  <span style="margin: 0px; background-color: #f8f8f8; font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 12px; color: red;">EXPERIMENTAL TECHNIQUE <span style="color: #000000;">from the above example using both Descendants and Ancestors method of LINQ 2 XML. I've tried</span></span></div><div><div class="dp-highlighter"><div class="bar"></div><ol start="1" class="dp-c"><li class="alt"><span><span>var item=from x </span><span class="keyword">in</span><span> doc.Descendants(</span><span class="string">"label"</span><span>)  </span></span></li><li class=""><span>                where x.Ancestors(<span class="string">"sec"</span><span>)  </span></span></li><li class="alt"><span>                select x;  </span></li></ol></div>but getting an error</div><div><u> Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to 'bool'</u></div> <br id="-moz-br-"></body></html>

Answers (3)