Gregor Menih
Gregor Menih

Reputation: 5126

c# - reading HTML?

I'm developing a program in C# and I require some help. I'm trying to create an array or a list of items, that display on a certain website. What I'm trying to do is read the anchor text and it's href. So for example, this is the HTML:

<div class="menu-1">
    <div class="items">
        <div class="minor">
            <ul>
                <li class="menu-item">
                    <a class="menu-link" title="Item-1" id="menu-item-1"
                    href="/?item=1">Item 1</a>
                </li>
                <li class="menu-item">
                    <a class="menu-link" title="Item-1" id="menu-item-2"
                    href="/?item=2">Item 2</a>
                </li>
                <li class="menu-item">
                    <a class="menu-link" title="Item-1" id="menu-item-3"
                    href="/?item=3">Item 3</a>
                </li>
                <li class="menu-item">
                    <a class="menu-link" title="Item-1" id="menu-item-4"
                    href="/?item=4">Item 4</a>
                </li>
                <li class="menu-item">
                    <a class="menu-link" title="Item-1" id="menu-item-5"
                    href="/?item=5">Item 5</a>
                </li>
            </ul>
        </div>
    </div>
</div>

So from that HTML I would like to read this:

string[,] array = {{"Item 1", "/?item=1"}, {"Item 2", "/?item=2"},
    {"Item 3", "/?item=3"}, {"Item 4", "/?item=4"}, {"Item 5", "/?item=5"}};

The HTML is an example I had written, the actual site does not look like that.

Upvotes: 3

Views: 17026

Answers (4)

Antonio Bakula
Antonio Bakula

Reputation: 20693

As others said HtmlAgilityPack is the best for html parsing, also be sure to download HAP Explorer from HtmlAgilityPack site, use it to test your selects, anyway this SelectNode command will get all anchors that have ID and it start with menu-item :

  HtmlDocument doc = new HtmlDocument();
  doc.Load(htmlFile);
  var myNodes = doc.DocumentNode.SelectNodes("//a[starts-with(@id,'menu-item-')]");
  foreach (HtmlNode node in myNodes)
  {
    Console.WriteLine(node.Id);

  }

Upvotes: 9

Helstein
Helstein

Reputation: 330

I think this case is simple enough to use a regular expression, like <a.*title="([^"]*)".*href="([^"]*)":

string strRegex = @"<a.*title=""([^""]*)"".*href=""([^""]*)""";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);

string strTargetString = ...;

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Use the groups matched
  }
}

Upvotes: 1

Gregoire
Gregoire

Reputation: 24882

You can also use the HtmlAgility pack

Upvotes: 1

MiMo
MiMo

Reputation: 11983

If the HTML is valid XML you can load it using the XmlDocument class and then access the pieces you want using XPaths, or you can use and XmlReader as Adriano suggests (a bit more work).

If the HTML is not valid XML I'd suggest to use some existing HTML parsers - see for example this - that worked OK for us.

Upvotes: 2

Related Questions