Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to be able to extract all the anchor tags from the bottom input string, however my current function, is only able to extract the last a href tag, as I would like to extract all of the anchor tags, under the heading 'picks'.

for example, here is my input feed going into bottom variable:
C#
string bottom = "<P align=justify>picks<BR>
<A href="/Article.aspx?article=froth&PUB=250&ISS=22787&SID=51610" name="" target=_blank>froth</A>
: Text...<BR>
<A href="/Article.aspx?article=Bouncing back&PUB=250&ISS=22787&SID=51603" name="" target=_blank>Bouncing back</A>
: Text...<BR></P>"


Updated server code:
C#
    var result = "";

    while (reader.Read())
    {
        string bottom = reader.GetString(1);

        string price = "";
        if (bottom.ToLower().Contains("picks"))
        {

            price = bottom.Substring(bottom.IndexOf("picks"), 5);

            HtmlDocument html = new HtmlDocument();

            html.LoadHtml(bottom);
            var anchors = html.DocumentNode.Descendants("A");

                foreach (var a in anchors)
                {
                    result = a.OuterHtml;
                }
          }

       result.ToString();

    }
    return article = result;
}




The following line of code is not allowing me add extract all() property, as it keeps throwing me this error - No overload for method 'All' takes 0 arguments

C#
if (anchors.All())


any further guide, as to where I may be going wrong would be very much appreciated.
Thanks
Posted
Updated 15-Apr-15 1:12am
v2

1 solution

The All method[^] verifies that all elements in the source collection match a particular predicate. It doesn't make any sense to call it without a predicate, so there isn't an overload to do that.

It's possible that you meant to call the Any method[^], which does have an overload without a predicate. However, given the code you've posted, you don't need to call any method; just use the foreach loop:
C#
var anchors = html.DocumentNode.Descendants("A");
foreach (var a in anchors)
{
   ...
}


EDIT:
To return all of the anchors, you'll either need to change the method to return a list of strings, or combine the strings using a separator:
C#
var result = new List<string>();

while (reader.Read())
{
    string bottom = reader.GetString(1);
    int index = bottom.IndexOf("picks", StringComparison.OrdinalIgnoreCase);
    if (index != -1)
    {
        var html = new HtmlDocument();
        html.LoadHtml(bottom);

        var anchors = html.DocumentNode.Descendants("A");
        foreach (var a in anchors)
        {
            result.Add(a.OuterHtml);
        }
    }
}

// To return a list of strings:
return result;

// To return a single string with each result on a new line (.NET 4.0 or higher):
return string.Join(Environment.NewLine, result);

// To return a single string with each result on a new line (.NET 3.5 or earlier):
return string.Join(Environment.NewLine, result.ToArray());
 
Share this answer
 
v2
Comments
miss786 15-Apr-15 4:42am    
Thank you for your feedback. I was originally only using foreach loop, but that only outputted the last anchor tag (1) from the string, however the string contains two anchor tags, which should be the correct output.
Any further guide, would be very helpful. Thanks
Richard Deeming 15-Apr-15 6:57am    
You haven't shown the code that's outputting the results.

Based on the code you've posted, you're overwriting a local variable called result on each iteration of the loop, which isn't going to work. How you solve it depends on what you're doing with result when the loop has finished.
miss786 15-Apr-15 7:14am    
I have updated my original post, showing how the result variable is being returned at the end of the loop. Is this correct? Thank for your time and help.
Richard Deeming 15-Apr-15 7:28am    
I've updated my answer. You need to add each item to a list, and then either return the list or combine the list into a single string somehow.
miss786 15-Apr-15 7:48am    
Thank you very much for your solution. Much appreciated.

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