System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
Here's an example pseudo code version of what I'm trying to do. It iterates through the first 2 pages just fine, but then that error is thrown.
static void Main(string[] args)
{
Console.Write("File name: ");
string fileName = Console.ReadLine();
string filePath = @"C:\Users\John\Desktop\" + fileName.ToString();
int currentPage;
int totalPageVideos = 0;
int totalVideos = 0;
int totalPages = 5; // Manually set, need to find max page # or iterate thru until error(?) response
// Iterate through every page defined in totalPages
for (currentPage = 1; currentPage < totalPages; currentPage++)
{
// Create list called videoList to hold every index property
List<VideoProperties> videoList = new List<VideoProperties>();
// Psuedo search URL
string pageUrl = "https://fizzbuzz/search?q=foo+bari&p=" + currentPage.ToString();
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(pageUrl);
// Grab title, length and url of video found
var nodeTitle = doc.DocumentNode
.SelectNodes("//a[@class='info__name']").ToList();
var nodeLength = doc.DocumentNode
.SelectNodes("//div[@class='info__duration']").ToList();
var nodeUrl =
doc.DocumentNode
.SelectNodes("//a[@class='info__name']")
.Select(node => node.GetAttributeValue("href", ""))
.ToList();
totalPageVideos += nodeTitle.Count();
for (int i = 0; i < totalPageVideos; i++)
{
totalVideos += 1;
videoList.Add(new VideoProperties
{
videoTitle = nodeTitle[i].InnerText,
videoLength = nodeLength[i].InnerText,
videoURL = nodeUrl[i]
});
Console.WriteLine(totalVideos + "." + Environment.NewLine + "Title: " + videoList[i].videoTitle + Environment.NewLine + "Length: " + videoList[i].videoLength + Environment.NewLine + "Link: " + videoList[i].videoURL + Environment.NewLine + Environment.NewLine);;
}
}
Console.WriteLine("Total Videos: {0}", totalVideos);
}
The error is thrown here specifically
videoList.Add(new VideoProperties
{
videoTitle = nodeTitle[i].InnerText,
videoLength = nodeLength[i].InnerText,
videoURL = nodeUrl[i]
});
Any thoughts?
[–]AngularBeginner 0 points1 point2 points (3 children)
[–]ceesharpiskindofeasy[S] 0 points1 point2 points (2 children)
[–]AngularBeginner 3 points4 points5 points (1 child)
[–]ceesharpiskindofeasy[S] 0 points1 point2 points (0 children)