I want to create a table where the ff. will show but I got some problems
public class Book
{
public HtmlAttribute Href{ get; set; }
public string Title{ get; set; }
public string Author{ get; set; }
public string Characters{ get; set; }
}
This is the page I am trying to parse, I need the href value, the link, the description and the character list (sometimes there is none):
<div id=title>
<li>
<h3><a href="www.harrypotter.com">Harry Potter</a></h3>
<div>Harry James Potter is the title character of J. K. Rowling's Harry Potter series. </div>
<ul>
<li>Harry Potter</li>
<li>Hermione Granger</li>
<li>Ron Weasley</li>
</ul>
</li>
<li>
<h3><a href="www.littleprince.com">Little Prince</a></h3>
<div>A little girl lives in a very grown-up world with her mother, who tries to prepare her for it. </div>
</li>
</div>
And this is my code to parse it and put it in a list
List<Book> BookList= new List<Book>();
var titleNode = doc.DocumentNode.SelectNodes("//*[@id=\"title\"]//li//h3");
var descNode = doc.DocumentNode.SelectNodes("//*[@id=\"title\"]//li//div");
var authorNode = doc.DocumentNode.SelectNodes("//*[@id=\"title\"]//li//ul");
var title = titleNode.Select(node => node.InnerText).ToList();
var desc = descNode.Select(node => node.InnerText).ToList();
var characters= authorNode.Select(node => node.InnerText).ToList();
for (int i = 0; i < Title.Count(); ++i)
{
var list= new Book();
list.Title= title[i];
list.Author= desc[i];
list.Characters = characters[i];
BookList.Add(list);
}
My questions are: 1) How will I get the href value and add it in the list? 2) Some have no tag for characters in the html, how can I get the list without an NullReferenceException error? Note: I can't make any changes in the html.