1

Currently having an issue where I cannot get C# to output my list into something which is readable, which means I cannot actually see if the webscraping is actually working or pulling incorrect information.

Anyone have any idea how I can change System.Collections.Generic.List1`[System.String] to something readable?

using HtmlAgilityPack;
using NScrape.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace FulcrumBotManager
{
    class Program
    {
        static void Main(string[] args)
        {

            WebClient webClient = new WebClient();
            string download = webClient.DownloadString("http://localhost:1013");

            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            html.LoadHtml(download);
            List<List<string>> table = html.DocumentNode.SelectSingleNode("//table")
                        .Descendants("tr")
                        .Skip(1)
                        .Where(tr => tr.Elements("td").Count() > 1)
                        .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
                        .ToList();


           table.ForEach(Console.WriteLine);
        }
    }
}

The HTML being scraped

<!DOCTYPE html>
<!-- saved from url=(0022)http://localhost:1013/ -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="refresh" content="5">
<style>#accounts {font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;border-collapse: collapse;margin: 0px auto;}#accounts td, #customers th {font-size: 1em;border: 1px solid #98bf21;padding: 3px 7px 2px 7px;}#accounts th {font-size: 1.1em;text-align: left;padding-top: 5px;padding-bottom: 4px;background-color: #A7C942;color: #ffffff;}#accounts tr.alt td {color: #000000;background-color: #EAF2D3;}</style>
</head>
<body>
<table id="accounts">
<tbody>
<tr>
<th>Run</th>
<th>Region</th>
<th>Username</th>
<th>Max IP</th>
<th>Game</th>
<th>Spell 1</th>
<th>Spell 2</th>
<th>Summoner</th>
<th>Lvl</th>
<th>Total IP</th>
<th>Total RP</th>
<th>Status</th>
</tr>
<tr>
<td>False</td>
<td>EUW</td>
<td>sage</td>
<td>0</td>
<td>ARAM</td>
<td>Barrier</td>
<td>Heal</td>
<td>Summoner</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>10:26:45 Disconnected</td>
</tr><tr class="alt">
<td>False</td>
<td>EUW</td>
<td>wily</td>
<td>0</td>
<td>ARAM</td>
<td>Barrier</td>
<td>Heal</td>
<td>Summoner</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>10:26:45 Disconnected</td>
</tr><tr>
<td>False</td>
<td>EUW</td>
<td>miles</td>
<td>0</td>
<td>ARAM</td>
<td>Barrier</td>
<td>Heal</td>
<td>Summoner</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>10:26:46 Disconnected</td>
</tr><tr class="alt">
<td>False</td>
<td>EUW</td>
<td>cookie</td>
<td>0</td>
<td>ARAM</td>
<td>Barrier</td>
<td>Heal</td>
<td>Summoner</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>10:26:47 Disconnected</td>
</tr><tr>
<td>False</td>
<td>EUW</td>
<td>lazors</td>
<td>0</td>
<td>ARAM</td>
<td>Barrier</td>
<td>Heal</td>
<td>Summoner</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>10:26:48 Disconnected</td>
</tr></tbody>
</table>
<center>Updated core files | 00:50:56:C0:00:01 00:50:56:C0:00:08 00:21:CC:73:5B:BF 08:11:96:F7:A7:0C 00:FF:8B:11:85:F4 <br>Refreshes every 5 seconds</center>
</body>
</html>
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Sage Hopkins
  • 183
  • 1
  • 3
  • 15
  • Well you've got a `List>` - how do you *want* to display that? (And please don't post C# code as if it's a Javascript runnable snippet...) – Jon Skeet Dec 04 '16 at 10:48
  • @JonSkeet Sorry about posting as javascript, i couldn't get the other code thing to actually highlight what I wanted. I want to just see what its pulling, it should be pulling 5 rows out of a table, id like to see what it has for each so that I can format it. – Sage Hopkins Dec 04 '16 at 10:49
  • So iterate over each outer list with a `foreach` loop, print something saying "this is the next row" then print out the inner list in whatever way you want to. It's not clear what's holding you back at the moment. – Jon Skeet Dec 04 '16 at 10:51
  • Duplicate of [Why am I getting System.Collections.Generic.List\`1\[System.String\] instead of the list's contents?](https://stackoverflow.com/questions/16106181/why-am-i-getting-system-collections-generic-list1system-string-instead-of-the) – TylerH Sep 10 '20 at 21:17

2 Answers2

2
table.ForEach(x => x.ForEach(Console.WriteLine));

The explanation of Martin is correct, just addition to it: You can do it like this with LINQ.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
1

Basically what you have is a list of lists of string :-) . That means it is "two levels of hierarchy".

In the current state you are just enumerating and writing each of the inner lists itself. Because Console.WriteLine is not familiar with Lists, it just calls ToString() on the instance, which outputs the type name.

What you actually want is to enumerate the inner list as well:

//enumerate all lists in the outer list
foreach ( var list in table )
{
   //enumerate the inner list
   foreach ( var item in list )
   {
        //output the actual item
        Console.WriteLine( item );
   }
}
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91