HtmlAgility tag replacement

html-agility-pack

Question

In an effort to upgrade all of myh1 tags alongh2 I'm using HTMLAgility Pack with tags.

What I did was

var headers = doc.DocumentNode.SelectNodes("//h1");
if (headers != null)
{
    foreach (HtmlNode item in headers)
    {
        //item.Replace??
    }
}

and I became trapped there. I've tried this

var headers = doc.DocumentNode.SelectNodes("//h1");
if (headers != null)
{
    foreach (HtmlNode item in headers)
    {
        HtmlNode newNode = new HtmlNode(HtmlNodeType.Element, doc, item.StreamPosition);
        newNode.InnerHtml = item.InnerHtml;
        // newNode suppose to set to h2
        item.ParentNode.ReplaceChild(newNode, item);
    }
}

The issue is that I'm unsure of how to make a new h2, get all the properties, etc. There must be an easy method to do it; any suggestions?

1
7
10/15/2017 12:26:37 AM

Popular Answer

var headers = doc.DocumentNode.SelectNodes("//h1");
        if (headers != null)
        {
            foreach (HtmlNode item in headers)
            {
                item.Name = "h2"
            }
        }
13
5/9/2011 11:39:49 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow