Skip to content Skip to sidebar Skip to footer

Get Div Class By Content Inside Div Using C#

I need to identify the class of a div element which contains some text. For example I have this HTML page ...

this is t

Solution 1:

Building on the answer of diiN_. This is a bit verbose but you should be able to get what you need from it. The code depends on the HTML Agility Pack. You can get it using nuget.

var sb = newStringBuilder();
sb.AppendFormat("<html>");
sb.AppendFormat("<div class='x'>");
sb.AppendFormat("<p>this is the text I have.</p>");
sb.AppendFormat("<p>Another part of text.</p>");
sb.AppendFormat("</div>");
sb.AppendFormat("</html>");

conststring stringToSearch = "<p>this is the text I have.</p><p>Another part of text.</p>";

vardocument = newHtmlDocument();
document.LoadHtml(sb.ToString());

var divsWithText = document
    .DocumentNode
    .Descendants("div")
    .Where(node => node.Descendants()
                       .Any(des => des.NodeType == HtmlNodeType.Text))
    .ToList();

var divsWithInnerHtmlMatching =
    divsWithText
        .Where(div => div.InnerHtml.Equals(stringToSearch))
        .ToList();

var innerHtmlAndClass =
    divsWithInnerHtmlMatching
        .Select(div =>new
            {
                InnerHtml = div.InnerHtml,
                Class = div.Attributes["class"].Value
            });

foreach (var item in innerHtmlAndClass)
{
Console.WriteLine("class='{0}' innerHtml='{1}'", item.Class, item.InnerHtml);
}

Solution 2:

Try this:

string stringToSearch = "<p>this is the text I have.</p><p>Another part of text.</p>";
HtmlDocumentdocument = newHtmlDocument();
document.LoadHtml(sb.ToString());

var classOfDiv = document.DocumentNode.Descendants("div").Select(x =>new
{
    ClassOfDiv = x.Attributes["class"].Value
}).Where(x => x.InnerHtml = stringToSearch);

The variable classOfDiv now contains the class name of the desired div.

Post a Comment for "Get Div Class By Content Inside Div Using C#"