c # HTMLAgilityPack supprimer noeuds

c# html html-agility-pack

Question

Je suis vraiment nouveau à utiliser HTMLAgilityPack . J'ai le document HTML suivant:

<a href="https://twitter.com/RedGiantNews" target="_blank"><img 
src="http://image.e.redgiant.com/lib/998.png" width="24" border="0" 
alt="Twitter" title="Twitter" class="smImage"></a><a 
href="https://www.facebook.com/RedGiantSoftware" target="_blank"><img 
src="http://image.e.redgiant.com/lib/db5.png" width="24" border="0" 
alt="Facebook" title="Facebook" class="smImage"></a>
http://click.e.redgiant.com/?qs=d2ad061f
<a href="https://www.instagram.com/redgiantnews/" target="_blank"><img 
src="http://image.e.redgiant.com/aa10-f8747e56f06d.png" width="24" 
border="0" alt="Instagram" title="Instagram" class="smImage"></a>

J'essaie de supprimer toutes les images, je veux dire tous les nœuds (si c'est le bon mot) de <img....> du fichier html. J'ai essayé le code ci-dessous d'une autre solution sur StackOverflow mais en vain car il renvoie le même HTMl que ci-dessus:

var sb = new StringBuilder();
doc.LoadHtml(inputHTml);

foreach (var node in doc.DocumentNode.ChildNodes)
{
 if (node.Name != "img" && node.Name!="a")
  {
    sb.Append(node.InnerHtml);
  }
}

Réponse acceptée

static string OutputHtml = @"<a href=""https://twitter.com/RedGiantNews"" target=""_blank""><img 
                                    src=""http://image.e.redgiant.com/lib/998.png"" width=""24"" border=""0"" 
                                    alt=""Twitter"" title=""Twitter"" class=""smImage""></a><a
                                    href = ""https://www.facebook.com/RedGiantSoftware"" target=""_blank""><img
                                    src = ""http://image.e.redgiant.com/lib/db5.png"" width=""24"" border=""0"" 
                                    alt=""Facebook"" title=""Facebook"" class=""smImage""></a>
                                    <a href = ""https://www.instagram.com/redgiantnews/"" target=""_blank""><img
                                    src = ""http://image.e.redgiant.com/aa10-f8747e56f06d.png"" width=""24"" 
                                    border=""0"" alt=""Instagram"" title=""Instagram"" class=""smImage""></a>";

J'ai supprimé le lien flottant ( http://click.e.redgiant.com/?qs=d2ad061f ) de la chaîne html d'origine.

Première approche:

public static string RemoveAllImageNodes(string html)
    {
        try
        {
            HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);

            var nodes = document.DocumentNode.SelectNodes("//img");

            foreach (var node in nodes)
            {
                node.Remove();
                //node.Attributes.Remove("src"); //This only removes the src Attribute from <img> tag
            }

            html = document.DocumentNode.OuterHtml;
            return html;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Approche Deux:

public static string RemoveAllImageNodes(string html)
    {
        try
        {
            HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);

            if (document.DocumentNode.InnerHtml.Contains("<img"))
            {
                foreach (var eachNode in document.DocumentNode.SelectNodes("//img"))
                {
                    eachNode.Remove();
                    //eachNode.Attributes.Remove("src"); //This only removes the src Attribute from <img> tag
                }
            }

            html = document.DocumentNode.OuterHtml;
            return html;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

OutPut Html:

<a href="https://twitter.com/RedGiantNews" target="_blank"></a>
<a href="https://www.facebook.com/RedGiantSoftware" target="_blank"></a>
<a href="https://www.instagram.com/redgiantnews/" target="_blank"></a>

Html en sortie - Après avoir supprimé uniquement les attributs "src" des balises "img":

<a href="https://twitter.com/RedGiantNews" target="_blank"><img width="24" border="0" alt="Twitter" title="Twitter" class="smImage"></a>
<a href="https://www.facebook.com/RedGiantSoftware" target="_blank"><img width="24" border="0" alt="Facebook" title="Facebook" class="smImage"></a>
<a href="https://www.instagram.com/redgiantnews/" target="_blank"><img width="24" border="0" alt="Instagram" title="Instagram" class="smImage"></a>



Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi