MyDaftQuestions
MyDaftQuestions

Reputation: 4691

How to retrieve the value from HTML

I'm trying to extract the value from an HTML string in a console application.

This MSDN almost explains what I want, as does this Get html element by value

In my console application, my function looks like

    public void MyMethod()
    {
        var myValue = GetValueFromHtml("<a href=\"#\">My Link</a>");
    }

    internal string GetValueFromHtml(code)
    {
       //mycode   
       //returns My Link
    }

I can do this using string manipulation, splitting on the first > and then on this new values first '<'.

I guess I could also try and convert it to XDocument (although expections are thrown about illegal characters but that my be workable still) but overall it seems a little overkill? I tried using XElement but that complains since I have < tags

I'm asuming that I could use the HtmlElement.GetValue() to achieve the same thing but I don't know how when I'm not working an array/List?

Upvotes: 0

Views: 171

Answers (2)

w.b
w.b

Reputation: 11228

string html = "<a href=\"#\">My Link</a>";
XElement elem = XElement.Parse(html);
Console.WriteLine(elem.Value);  // output: My Link

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

I suggest you to use HtmlAgilityPack (available from NuGet):

string html = "<a href=\"#\">My Link</a>";
var node = HtmlNode.CreateNode(html);
string value = node.InnerHtml; // "My Link"

Upvotes: 3

Related Questions