Trying to find the fastest code to extract only the last 2 colums (all rows) from an html table. I've tried the below which works but I don;t want it to write to cdm window/console. It dissapears anyway. I want for example to use the values in another class or pass them on to something else. Also not sure what libraries to import always so plese show your libraries too.
[using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace ConsoleApplication3
{
class WebFetch
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(); //Used to StringBuilder entire input
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest) //Prepare the Webpage we will be asking for
WebRequest.Create("http://www.eia.gov/dnav/pet/pet_sum_sndw_dcus_nus_w.htm");
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
sb.Append(tempString);
}
}
while (count > 0);
Console.WriteLine(sb.ToString());
}
}
}
]
I;ve also tried this but getting errors:
[using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
class Program2
{
static void Main(string[] args)
{
public String GetTableSource()
{
//Get the Search Address
String address = @"http://www.eia.gov/dnav/pet/pet_sum_sndw_dcus_nus_w.htm";
//Create an Instance of the WebClient Class
System.Net.WebClient client = new System.Net.WebClient();
//Download the Source
String reply = client.DownloadString(address);
//Will remove everything up to the table, Assuming there is only one Table on the Page
reply = reply.Remove(0, reply.IndexOf("<table"));
//Remove everything after </table>
reply = reply.Remove(reply.IndexOf("</table>") + 8);
//Return the Source
return reply;
}
}
}]

New Topic/Question
Reply



MultiQuote



|