Click here to Skip to main content
15,900,706 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, i am trying to check on a webpage for a specific link that whether it is on that page or not. I am trying to convert that tagList to char array and the string url 1st and then i am checking whether it is equal or not(equal in a sense char by char), not by string by string. I want it to check the url i already have hard coded with the all available on any page. As soon as it gets the one i hard coded it should say found. Kindly help if there can be made any changes in this code, or if somewhere i am missing.
Regards.
XML
private void tags_get()
        {

            List<string> tagList = new List<string>();

            List<string> linkList = new List<string>();

            //char[] c_arr;

            string url="http://www.adireo.com/";
            List<char> newUrl = new List<char>(url);
            //Convert.ToChar(newUrl);
            foreach (HtmlElement link in webBrowserCtl.Document.Links)
            {

                linkList.Add(link.GetAttribute("href").ToString());

                tagList.Add(link.InnerText);

            }

            List<string> new_tagList = new List<string>(tagList);
            //Convert.ToChar(new_tagList);
            List<char> url_converted = new List<char>(newUrl);
            for (int a = 0; a < url_converted.Count; a++)
            {
                if (!(new_tagList).Contains(url_converted))
                {
                    MessageBox.Show("found");
                }
                else
                {
                    MessageBox.Show("Not Found");
                }
            }
           }
          }
Posted
Updated 23-Aug-12 0:33am
v2

You can use SequenceEqual since you are using c#4.0

Check this.

http://msdn.microsoft.com/en-us/library/bb348567.aspx[^]

or if you want your own method, try this.
C#
static bool ArraysEqual<T>(T[] a1, T[] a2)
{
    if (ReferenceEquals(a1,a2))
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!comparer.Equals(a1[i], a2[i])) return false;
    }
    return true;
}
 
Share this answer
 
v2
Comments
Member 9357064 23-Aug-12 6:46am    
Thanks for the quick reply. I have a question here that i need to compare the characters in my case. how to do that?
To clarify, it appears you are checking if the text of the link matches url, not if the target of the link matches?
So a link like:
<a href="http://google.com/">http://www.adireo.com/</a>
should be a match?

I don't see the reason that you need to compare char-by-char. StringComparison.Ordinal will do the most-exact comparison.

If you just want a true/false that some link matches url try something like this:
C++
// requires: using System.Linq;
private void tags_get()
{
  const string url="http://www.adireo.com/";
  var links = webBrowserCtl.Document.Links;
  bool found = links.Any(link => string.Equals(link.InnerText, url, System.StringComparison.Ordinal));
  // or, if you really intend to compare the link target:
  //bool found = links.Any(link => string.Equals(link.GetAttribute("href"), url, System.StringComparison.Ordinal));
  MessageBox.Show(found ? "found" : "Not Found");
}

Assuming you want to do something with the links after determining that url is/isn't present, you could get a Dictionary of the links keyed by the text or target:
C++
private void tags_get()
{
  Dictionary<string, HtmlElement> linkMap = new Dictionary<string, HtmlElement>(StringComparer.Ordinal);
  const string url="http://www.adireo.com/";
  foreach (HtmlElement link in webBrowserCtl.Document.Links)
  {
    string key = link.InnerText;  // link text
    // or
    //string key = link.GetAttribute("href"); // link target
    if (linkMap.ContainsKey(key))
    {
      // this link occurs multiple times, handle appropriately!
    }
    linkMap.Add(key, link);
  }

  if (linkMap.ContainsKey(url))
  {
    MessageBox.Show("found");
  }
  else
  {
    MessageBox.Show("Not Found");
  }
  // do additional processing of the links ...
}
 
Share this answer
 
Comments
Member 9357064 23-Aug-12 16:06pm    
Thank you very mush for the complete help. I got where i was missing.
Regards.

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