Skip to content Skip to sidebar Skip to footer

Parsing Html - How To Get A Number From A Tag?

I am developing a Windows Forms application which is interacting with a web site. Using a WebBrowser control I am controlling the web site and I can iterate through the tags using

Solution 1:

You can try splitting a string by ';' values, and then each string by '=' like this:

string aTag = ...;
foreach(var splitted in aTag.Split(';'))
{
   if(splitted.Contains("="))
   {
      var leftSide = splitted.Split('=')[0];
      var rightSide = splitted.Split('=')[1];
      if(leftSide == "assignedto")
      {
          MessageBox.Show(rightSide); //It should be 244//Or...int num = int.Parse(rightSide);
      }
   }
}

Other option is to use Regexes, which you can test here: www.regextester.com. And some more info on regexes: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

Hope it helps!

Solution 2:

If all cases are similar to this and you don't mind a reference to System.Web in your Windows Forms application, tou can do something like this:

using System;

publicclassProgram
{
    staticvoidMain()
    {
        string href = @"issue?status=-1,1,2,3,4,5,6,7&
@sort=-activity&@search_text=&@dispname=Show Assigned&
@filter=status,assignedto&@group=priority&
@columns=id,activity,title,creator,status&assignedto=244&
@pagesize=50&@startwith=0";

        href = System.Web.HttpUtility.HtmlDecode(href);

        var querystring = System.Web.HttpUtility.ParseQueryString(href);

        Console.WriteLine(querystring["assignedto"]);
    }
}

This is a simplified example and first you need to extract the href attribute text, but that should not be complex. Having the href attribute text you can take advantage that is basically a querystring and reuse code in .NET that already parses query strings.

To complete the example, to obtain the href attribute text you could do:

HtmlElementCollection aTags = webBrowser.Document.GetElementsByTagName("a");

foreach (HtmlElement element in aTags)
{
    string href = element.GetAttribute("href");
}

Post a Comment for "Parsing Html - How To Get A Number From A Tag?"