質問

私はhtmlページを取得するために使用していることを、次のコードを持っています。 URLは絶対作り、その後、リンクはnofollowを新しいウィンドウ/タブで開いRELします。私の問題は<a>sに属性を追加する程度である。

        string url = "http://www.mysite.com/";
        string strResult = "";            

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        if ((request.HaveResponse) && (response.StatusCode == HttpStatusCode.OK)) {
            using (StreamReader sr = new StreamReader(response.GetResponseStream())) {
                strResult = sr.ReadToEnd();
                sr.Close();
            }
        }

        HtmlDocument ContentHTML = new HtmlDocument();
        ContentHTML.LoadHtml(strResult);
        HtmlNode ContentNode = ContentHTML.GetElementbyId("content");

        foreach (HtmlNode node in ContentNode.SelectNodes("/a")) {
            node.Attributes.Append("rel", "nofollow");
            node.Attributes.Append("target", "_blank");
        }

        return ContentNode.WriteTo();

缶誰もが私が間違ってやっているものを参照してください?運とここしばらく試してき。このコードはContentNode.SelectNodesが(「/」)オブジェクトのインスタンスに設定されていないことをアップしています。 I 0?

に蒸気を試してみて、設定しても

乾杯、 デニス

役に立ちましたか?

解決

ContentNodeヌルですか?あなたは、クエリ"//*[@id='content']"と、単一選択する必要があるかもしれません。

情報については、"/a"はルートのののすべてのアンカーを意味します。 "descendant::a"動作しますか? 。より簡単かもしれませんHtmlElement.GetElementsByTagNameもあります - すなわちyourElement.GetElementsByTagName("a")
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top