TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
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"
>
<
front
>
<
title-group
>
<
title
>
SMALL angle scattering polarization biopsy: a comparative analysis of various skin diseases
</
title
>
</
title-group
>
....
....
</
front
>
<
body
>
<
boxed-text
id
=
"s1"
position
=
"anchor"
>
<
sidebar-label
>
S1.
</
sidebar-label
>
<
sec
id
=
"sec1"
>
<
title
>
EXPERIMENTAL TECHNIQUE
</
title
>
<
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
>
</
sec
>
</
boxed-text
>
....
....
</
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
var item=from x
in
doc.Descendants(
"label"
)
where x.Ancestors(
"sec"
)
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>
Reply
Answers (
3
)
SQL Server date format.
How to group multiple array of object in c#