views:

216

answers:

1

Hey all,

I've got the following code that I'm using to get a html page. Make the urls absolute and then make the links rel nofollow and open in a new window/tab. My issue is around the adding of the attributes to the <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();

Can anyone see what I'm doing wrong? Been try for a while here with no luck. This code comes up that ContentNode.SelectNodes("/a") isn't set to an instance of an object. I though to try and set the steam to 0?

Cheers, Denis

+1  A: 

Is ContentNode null? You might need to select-single with the query "//*[@id='content']".

For info, "/a" means all anchors at the root. does "descendant::a" work? There is also HtmlElement.GetElementsByTagName which might be easier - i.e. yourElement.GetElementsByTagName("a").

Marc Gravell
It all came now to the XPath/XSL, descendant::a sorted iut for me! Thanks. I knew / was the root but didn't notice. Thanks
Denis Hoctor