Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to get specific string on an HTML then display it on label.

PS: the code that i have posted is only an example to shortened the explanation/question. What i really want to get is a text/string on a msg file HTMLBody.

What I have tried:

in this example i want to get "SAMPLE" string then display it on the label under the button:

C#
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        THIS IS A 
        <label>SAMPLE</label>
        TEXT
        <br />
        <button>GET</button>
        <br />
        <label>LABEL</label>
    </div>
    </form>
</body>
</html>
Posted
Updated 17-Feb-16 16:27pm
v4
Comments
Maciej Los 17-Feb-16 14:37pm    
What have you tried?
Where are you stuck?
Afzaal Ahmad Zeeshan 17-Feb-16 14:44pm    
I would recommend, that you capture the text only and not the HTML of the element. The text representation would be, "SAMPLE", when HTML representation would depend on the element.
Member 11525563 17-Feb-16 15:47pm    
can you give me an example on how to do it on c#?
yeleswarapu ramakrishna 17-Feb-16 14:53pm    
this is very easy try it yourself
PIEBALDconsult 17-Feb-16 15:11pm    
Learn thee XPATH
http://www.w3schools.com/xsl/xpath_intro.asp

1 solution

Try HTMLagilityPACK

C#
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

// There are various options, set as needed
htmlDoc.OptionFixNestedTags=true;

// filePath is a path to a file containing the html
htmlDoc.Load(filePath);

// Use:  htmlDoc.LoadHtml(xmlString);  to load from a string (was htmlDoc.LoadXML(xmlString)

// ParseErrors is an ArrayList containing any errors from the Load statement
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() &gt; 0)
{
    // Handle any parse errors as required

}
else
{

    if (htmlDoc.DocumentNode != null)
    {
        HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");

        if (bodyNode != null)
        {
            // Do something with bodyNode
        }
    }
}



Scraping HTML DOM elements using HtmlAgilityPack (HAP) in ASP.NET[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900