3

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.

2
  • Did you try to take a look to the XmlTextReader stream? You'll catch all the a and - plus - it's quick even with a big XML file. Commented May 22, 2012 at 20:11
  • 5
    Check out the HtmlAgilityPack Commented May 22, 2012 at 20:12

4 Answers 4

9

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);

  }
Sign up to request clarification or add additional context in comments.

2 Comments

The HAP Explorer ? Where do you get that from ?
2

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.

1 Comment

1

You can also use the HtmlAgility pack

Comments

1

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
  }
}

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.