1

I want to know how to get the text from a DIV with align=left style?

Here is my code so far of my xpath query, my only problem is in here I dont know if my xpath query is correct.

I think the problem is in the [@align='left]' code of my xpath query.

var nodes = doc.DocumentNode.SelectNodes("//div[@align='left']");
5
  • Looks OK. What's the problem? Any errors? Exceptions? What exactly is not working? The nodes variable should contain a node collection with those nodes.
    – Oded
    Apr 4, 2012 at 14:06
  • i think @align='left' in the code is not correct..i'm not getting the text in the div tag
    – guitarPH
    Apr 4, 2012 at 14:12
  • How have you accessed the text of each of the selected nodes? You didn't who that code.
    – Oded
    Apr 4, 2012 at 14:13
  • Attribute value selectors were added recently I believe... They used to be unsupported. Which version of HAP are you using? Apr 4, 2012 at 17:23
  • I'm using the latest HAP
    – guitarPH
    Apr 6, 2012 at 15:37

1 Answer 1

0

HtmlNode.SelectNodes method selects a list of nodes matching the xpath expression passed. You need to iterate these nodes and get the InnerHtml (or InnerText) of each item like this:

// check if nodes found
if (nodes != null)
    foreach (var div in nodes)
    {
        var html = div.InneHtml;
    }
4
  • Thanks for your answer but I already have that in my code. My only problem is in the xpath query.
    – guitarPH
    Apr 4, 2012 at 14:16
  • @user1307655, your xpath looks OK. I tried it out, and it selects the divs with align attribute set to left for me.
    – Oleks
    Apr 4, 2012 at 14:46
  • Make sure you check for a null outcome. The HAP returns null, not an empty collection if it can't find any nodes: var nodes = (doc.DocumentNode.SelectNodes("//div[@align='left']") ?? new HtmlNodeCollection(null)); Apr 4, 2012 at 17:32
  • @jessehouwing, there is already a check for null in the code snippet I provided
    – Oleks
    Apr 4, 2012 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.