SandhanaMurali
SandhanaMurali

Reputation: 149

how to find a image tags in source code using C#?

I am working with c# now stored webpage content in single variable and I have one text box if I paste any URL that will show the full source code the link .now I want to find all the image tags where it is begin and where it is finished.also I like to merge except image tags .

can you anyone tell me how to do..

Upvotes: 1

Views: 455

Answers (2)

sajanyamaha
sajanyamaha

Reputation: 3198

Try This:

var images = doc.DocumentNode.SelectNodes("//img");
if (images != null)
{
    foreach (HtmlNode image in images)
    {
        var alt = image.GetAttributeValue("alt", "");
        var nodeForReplace = HtmlTextNode.CreateNode(alt);
        image.ParentNode.ReplaceChild(nodeForReplace, image);
    }
}

var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
    doc.Save(writer);
}

Upvotes: 1

JP.
JP.

Reputation: 5606

Assuming you want to parse the content server-side you can use the Html Agility pack

See this question

Upvotes: 3

Related Questions