c# - Why my code is selecting all text() nodes in Htmldocument -


    htmlnode node = doc.documentnode.selectnodes("//tr")[0];             foreach(htmltextnode n in node.selectnodes("//text()"))                console.writeline(n.text); 

html:

<table class="infobox" style="width: 17em; font-size: 100%;float: left;"> <tr> <th style="text-align: center; background: #f08080;" colspan="3">خدیجہ مستور</th> </tr> <tr style="text-align: center;"> <td colspan="3"><a href="/wiki/%d8%aa%d8%b5%d9%88%db%8c%d8%b1:khatijamastoor.jpg" class="image" title="خدیجہ مستور"><img alt="خدیجہ مستور" src="//upload.wikimedia.org/wikipedia/ur/thumb/7/7b/khatijamastoor.jpg/150px-khatijamastoor.jpg" width="150" height="203" srcset="//upload.wikimedia.org/wikipedia/ur/thumb/7/7b/khatijamastoor.jpg/225px-khatijamastoor.jpg 1.5x, //upload.wikimedia.org/wikipedia/ur/thumb/7/7b/khatijamastoor.jpg/300px-khatijamastoor.jpg 2x"></a><br> <div style="font-size: 90%">خدیجہ مستور</div> </td> </tr> <tr> <th style="background: #f08080;" colspan="3">ادیب</th> </tr> <tr> <td><b>ولادت</b></td> <td colspan="2"><a href="/wiki/1930%d8%a1" title="1930ء">1930ء</a>، <a href="/wiki/%d9%84%da%a9%da%be%d9%86%d8%a4" title="لکھنؤ">لکھنؤ</a>، <a href="/wiki/%d8%a8%d8%b1%d8%b7%d8%a7%d9%86%d9%88%db%8c_%db%81%d9%86%d8%af%d9%88%d8%b3%d8%aa%d8%a7%d9%86" title="برطانوی ہندوستان" class="mw-redirect">برطانوی ہندوستان</a></td> </tr> <tr> <td><b><a href="/wiki/%d8%a7%d8%b5%d9%86%d8%a7%d9%81_%d8%a7%d8%af%d8%a8" title="اصناف ادب">اصناف ادب</a></b></td> <td colspan="2"><a href="/wiki/%d9%86%d8%a7%d9%88%d9%84" title="ناول" class="mw-redirect">ناول</a></td> </tr> <tr> <td><b>معروف تصانیف</b></td> <td colspan="2"><a href="/wiki/%d8%a2%d9%86%da%af%d9%86" title="آنگن">آنگن</a></td> </tr> </table> 

output should :

خدیجہ مستور

but found :

خدیجہ مستور

خدیجہ مستور

ادیب

ولادت

1930ء ، لکھنؤ ، برطانوی ہندوستان

اصناف ادب

ناول

معروف تصانیف

آنگن

why node.selectnodes("//text()") selecting text() nodes in document rather text() nodes first tr tag??

because adding 2 forward slashes beginning of xpath (//tr), selects of elements in document, not descendants of selected node.

try instead:

foreach (htmltextnode n in node.selectnodes("text()")) 

or simplify xpath to:

var node = doc.documentnode.selectsinglenode("//tr[1]/text()"); console.writeline(node.text); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -