Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么Xpath htmlagilitypack选择不起作用_C#_Xpath_Html Agility Pack - Fatal编程技术网

C# 为什么Xpath htmlagilitypack选择不起作用

C# 为什么Xpath htmlagilitypack选择不起作用,c#,xpath,html-agility-pack,C#,Xpath,Html Agility Pack,好的,这里是源代码和我的xpath 我试图选择第三个值,但它不工作,即使选择第一个值的工作 HtmlDocument hdMyDoc = new HtmlDocument(); hdMyDoc.LoadHtml(File.ReadAllText("html.txt")); HtmlNode hdNodes = hdMyDoc.DocumentNode.SelectSingleNode (string.Format("//div[@

好的,这里是源代码和我的xpath

我试图选择第三个值,但它不工作,即使选择第一个值的工作

        HtmlDocument hdMyDoc = new HtmlDocument();
        hdMyDoc.LoadHtml(File.ReadAllText("html.txt"));

        HtmlNode hdNodes = hdMyDoc.DocumentNode.SelectSingleNode
        (string.Format("//div[@class='rating'][3]", "div", "class", "rating"));
但是,当我编写
[1]
时,它确实起作用

源代码

<div class="anatablo">
    <div class="helpful">
        Bu inceleme sizce faydalı mı?&nbsp; <button class="yesbutton"
        onclick=
        "location.href='http://www.teknobiyotik.com/advancedreviews/helpfulness/post/reviewId/3403/actionName/Yes/';"
        type="button"></button> <button class="nobutton" onclick=
        "location.href='http://www.teknobiyotik.com/advancedreviews/helpfulness/post/reviewId/3403/actionName/No/';"
        type="button"></button>
    </div>

    <div class="baslik">
        Yorumlayan : Ufuk&nbsp;
    </div>

    <table class="alttablo">
        <tbody>
            <tr>
                <td>
                    <table>
                        <tbody>
                            <tr>
                                <td class="yildiz">
                                    <table cellspacing="0" class=
                                    "ratings-list">
                                        <tbody>
                                            <tr>
                                                <td class="label" style=
                                                "font-weight: bold">
                                                Fiyat</td>
                                            </tr>

                                            <tr>
                                                <td>
                                                    <div class="rating-box"
                                                    style=
                                                    "margin-bottom:3px;">
                                                        <div class="rating"
                                                        style=
                                                        "width: 33%;">
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td class="label" style=
                                                "font-weight: bold">
                                                Kullanım Kolaylığı</td>
                                            </tr>

                                            <tr>
                                                <td>
                                                    <div class="rating-box"
                                                    style=
                                                    "margin-bottom:3px;">
                                                        <div class="rating"
                                                        style=
                                                        "width: 66%;">
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td class="label" style=
                                                "font-weight: bold">
                                                Kalite</td>
                                            </tr>

                                            <tr>
                                                <td>
                                                    <div class="rating-box"
                                                    style=
                                                    "margin-bottom:3px;">
                                                        <div class="rating"
                                                        style=
                                                        "width: 99%;">
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>

                                <td class="yorumalani">
                                    <div class="yorumbaslik">
                                        <strong>Kocaman birşey</strong> (
                                        06.07.2013 )
                                    </div>Bu fiyata akasa ürünü hem de
                                    isteyebileceğinizden daha büyük bir
                                    mouse alanı. Yere adeta yapışıyor.
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>

    <table width="100%">
        <tbody>
            <tr>
                <td>
                    <div class="review-footer" style="display:block;">
                        <table style="width:100%;">
                            <tbody>
                                <tr>
                                    <td class="socialshare">
                                        <!-- AW_AdvancedReviews Socialshare block start -->
                                        Bu incelemeyi paylaşın <a href=
                                        "http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.teknobiyotik.com%2Freview%2Fproduct%2Fview%2Fid%2F3403%2F%3F___SID%3DU&amp;t=Kocaman%20bir%C5%9Fey"
                                        target="_blank"><img alt="Facebook"
                                        height="16" src=
                                        "http://www.teknobiyotik.com/skin/frontend/default/default/advancedreviews/images/link-facebook.gif"
                                        title="Add to Facebook" width=
                                        "16"></a> 
                                        <!-- AW_AdvancedReviews Socialshare block end -->
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>

<ul></ul>

你要什么?
尤鲁姆莱安:乌夫克
菲亚特
库兰姆·科拉伊尔ııı
钾铁矿
Kocaman birşey(
06.07.2013 )
不,菲雅塔·阿卡萨·吕努赫姆·德
伊斯特耶比莱切登·达哈·比尔克
老鼠alanı。叶尔阿德塔·亚普·约尔。
不可能

    尝试使用

    hdMyDoc.DocumentNode.SelectSingleNode("(//div[@class='rating'])[3]");
    

    xpath中的括号非常重要。

    我不明白
    字符串.Format(“//div[@class='rating'][3],“div”,“class”,“rating”)
    的工作原理。也许你忘了带花括号了?也许您考虑过
    string.Format(“//div[@class='rating'][{2}]”,“div”,“class”,“rating”)
    。记住string.format使用基于0的占位符。@rraszewski//div[@class='rating'][1]”或//div[@class='rating']”很好地工作。我仍然不明白为什么要使用
    string.format
    和参数
    “div”,“class”,“rating”
    ,并且您没有任何占位符。@rraszewski忽略它:)它现在已经无效了。你能详细解释一下()的区别吗?直到现在我才使用过它,每次我都使用
    hdMyDoc.DocumentNode.SelectSingleNode(“//div[@class='rating'][1]”的东西
    根本不起作用,因为如果将“SelectSingleNode”更改为
    SelectNodes
    ,则始终会得到3个元素。因此,
    [1]
    被省略了。我测试了一些解决方案,当然也浏览了网络。到处看看