2
votes

I have something like this

<div class='mainclass subclass1' quest-id='123'> </div>
<div class='mainclass subclass2' quest-id='234'> </div>
<input quest-id='3236'> </input>
<textarea quest-id='256'> </textarea>

I want the quest-d of all div which belongs to class named subclass1, subclass2 and quest-id of all input and textarea. How can i do it using html agility pack in c#?

and i have c# code like this:

HtmlDocument document = new HtmlDocument();
document.LoadHtml(obj.NewPage.Content);

HtmlNode htmlRootElement = document.DocumentNode.SelectSingleNode("/html");
HtmlNode bodyElement = htmlRootElement.SelectSingleNode("body");

i dont know how to proceed

3

3 Answers

0
votes

Following XPath can get all quest-id value from sample html posted in this question :

//div[contains(@class, 'subclass1') or contains(@class, 'subclass2')]
| //input[@quest-id] 
| //textarea[@quest-id]

Working example :

var html = @"<root><div class='mainclass subclass1' quest-id='123'> </div>
<div class='mainclass subclass2' quest-id='234'> </div>
<input quest-id='3236'> </input>
<textarea quest-id='256'> </textarea></root>";
var doc = new HtmlDocument();
doc.LoadHtml(html);

var nodes = 
    doc.DocumentNode
       .SelectNodes("//div[contains(@class, 'subclass1') or contains(@class, 'subclass2')]"
                        + " | //input[@quest-id] "
                        + " | //textarea[@quest-id]");
foreach (var node in nodes)
{
    Console.WriteLine(node.GetAttributeValue("quest-id", ""));
}
0
votes

This is the snippet I wrote and tested.

 const string sampleHTML = @"<div class='mainclass subclass1' quest-id='123'></div>
    <div class='mainclass subclass2' quest-id='234'></div>
    <input quest-id='3236'> </input>
    <textarea quest-id='256'> </textarea>";


 HtmlAgilityPack.HtmlDocument myDoc = new HtmlAgilityPack.HtmlDocument();
 myDoc.LoadHtml(sampleHTML);
 HtmlNodeCollection foundNodes = myDoc.DocumentNode.SelectNodes("/div[contains(@class, 'subclass2')]");                                                                         
 MessageBox.Show(foundNodes[0].Attributes["quest-id"].Value);

When I run the snippet, I see the value '234' in the message box.

0
votes
 string id = div.GetAttributeValue("id", "").ToString();
 string name= div.GetAttributeValue("name", "").ToString();