Prix
Prix

Reputation: 19528

Replacing an attribute with AgilityHTML?

I need to change the css of lots of pages and so I took the chance to play with AgilityHTML, I can read the css entries that I have to change just fine but I have no idea how to change the href of it.

here is an example of what I wanted to change:

<link rel="stylesheet" type="text/css" href="http://cdn.mysite.com/master/public.css?rev=012010">

More specific the href:

http://cdn.mysite.com/master/public.css?rev=012010

I've looked around but havent found the answer yet.

var nodes = doc.DocumentNode.SelectNodes("//css[@type=\"text/css\"]");
if (nodes != null)
{
    foreach (HtmlNode data in nodes)
    {
        if (data.Attributes["href"] == null)
            continue;

        //data.Attributes["href"].Value;
    }
}

To resume:

How could I change the href and save it back ?

Upvotes: 1

Views: 172

Answers (2)

crypted
crypted

Reputation: 10306

Try following,

var nodes = doc.DocumentNode.SelectNodes("//css[@type='text/css']");

It will select the nodes correctly.

I guess there is method on HtmlNode class called

SetAttributeValue

you can use it to save the new value.

Once you set the value you can access changed html content using

node.DocumentNode.OuterHtml

Upvotes: 0

jessehouwing
jessehouwing

Reputation: 114651

data.Attributes["href"].Value = "Whatever you want";
...
...
doc.Save(stream);
// or:
string content = doc.DocumentNode.OuterHtml;

Upvotes: 2

Related Questions