开发者

How select multiple tags

I've this code

<div id="teste">
<table>
<tbody>
<tr> "Lots of HTML TAGS ex: img, href, etc" </tr>
<tr> "Lots of HTML TAGS" </tr>
<tr> "Lots of HTML TAGS" </tr>
<tr> "Lots of HTML TAGS" </tr>
</tbody>
</table>
</div>

Inside the TR there's a lot of other code.. Sometimes TR increases : ex: Page 1 there's 5 tr | Page 2 there's 8 tr

I want to take all the TR that are inside the div "teste"-table-tbody... that's it..

I trying

doc.DocumentNode.SelectNodes("//div[@id='teste']/table/tbody/tr");

I want to catch ALL TR tags including other Html tags that are inside the TR

HERE ARE THE CODE THAT IN USE

<td align="left" class="portal_table_info_th">
   <div id="teste" style="displ开发者_开发百科ay:inline;" class="smallfont">
      <table width="100%" border="0" cellspacing="2" cellpadding="0">
         <tbody>

           <tr>
           <td class="smallfont alt2">
           <b><center>TUTORIAL CSS</center></b>
           </td>
           <td width="30" class="smallfont">
           <span style="color:#000000; font-weight:bold">STATUS</span>
           </td>
           </tr>


           <tr>
           <td class="smallfont alt1"><center><a href="" target="_blank">Tutorial 1</a></center></td>
           <td width="30" class="smallfont"><span style="color:#009933; font-weight:bold">ON</span></td>
           </tr>


           <tr>
           <td class="smallfont alt2"><center><a href="" target="_blank">Tutorial 2</a></center></td>
           <td width="30" class="smallfont"><span style="color:#009933; font-weight:bold">ON</span></td>
           </tr>


           <tr>
           <td class="smallfont alt1"><center><a href="" target="_blank">Tutorial 3</a></center></td>
           <td width="30" class="smallfont"><span style="color:#009933; font-weight:bold">ON</span></td>
           </tr>


           <tr>
           <td class="smallfont alt2"><center><a href="" target="_blank">Tutorial 4</a></center></td>
           <td width="30" class="smallfont"><span style="color:#009933; font-weight:bold">ON</span></td>
           </tr>


           <tr>
           <td class="smallfont alt1"><center><a href="" target="_blank">Tutorial 5</a></center></td>
           <td width="30" class="smallfont"><span style="color:#009933; font-weight:bold">ON</span></td>
           </tr>

        </tbody>
   </table>
  </div>
</td>

HERE are other information... when i put

doc.DocumentNode.SelectNodes("//div[@id='teste']/table/tbody/tr");

DONT WORK , return a null value. BUT WHEN I PUT

doc.DocumentNode.SelectNodes("//div[@id='teste']");

Its works... :/ , But takes all tags.

I try use this and my doc is - ( the data is a HttpGET )

doc = new HtmlDocument();
doc.LoadHtml(data);
.
.
.
HtmlNodeCollection trtag = doc...
string trtag = doc...


Two issues:

1) add quotes around the value for id, otherwise it will not be a valid xml markup i.e. id="teste"(this is first reason for the issue and once changed you will get the nodes)

2) Remove the *.

doc.DocumentNode.SelectNodes("//div[@id='teste']/table/tbody/tr");

should work..

EDIT: To extract all the tr nodes except the last one use the xpath "//div[@id='teste']/table/tbody/tr[position()<last()]" (notice the [position() )

A sample to extract the nodes and convert to string.

static void ExtractNodeInfo()
        {
            String xmlText = @"<div id=""teste""> <table> <tbody> <tr> ""Lots of HTML TAGS ex: img, href, etc"" </tr> <tr> ""Lots of HTML TAGS"" </tr> <tr> ""Lots of HTML TAGS"" </tr> <tr> Last ""Lots of HTML TAGS"" </tr> </tbody> </table> </div> ";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlText);
            XmlNode root = doc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("//div[@id='teste']/table/tbody/tr[position()<last()]");
            StringBuilder selectedNodesOuter = new StringBuilder();
            StringBuilder selectedNodesInner = new StringBuilder();
            foreach(XmlNode node in nodes)
            {
                selectedNodesOuter.Append(node.OuterXml);
                selectedNodesInner.Append(node.InnerXml);
            }
            Console.WriteLine("######### OUTER XML #########");
            Console.WriteLine("");
            Console.WriteLine(selectedNodesOuter);
            Console.WriteLine("");
            Console.WriteLine("######### INNER XML #########");
            Console.WriteLine("");
            Console.WriteLine(selectedNodesInner);
        }


Nathan you can use one of the thousand XPath tester online: http://www.futurelab.ch/xmlkurs/xpath.en.html this allows you to check quickly what you get whenever you change your xpath query, faster than waiting for an answer here :)


what's wrong with:

trArray=document.getElementById('teste').getElementsByTagName('tr');

which will give you an array of all the TRs (assuming there's no tables inside the TRs)

If you want to find tags inside them then you can do something like

tagArray=trArray[0.getElementsByTagName('img');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜