Парсинг веб-страницы с использованием Html Agility Pack - C#
Формулировка задачи:
Доброго времени суток! Задумал я парсить курсы валют с сайта Банка России. Считывать получается, но, судя по внешнему виду получившегося кода, кажется, что можно это все сделать намного проще.
Подскажите, пожалуйста, как упростить код и сделать его более красивым, если это возможно, конечно. Спасибо.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
namespace parser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void calculate_Click(object sender, EventArgs e)
{
var webGet = new HtmlWeb();
var document = webGet.Load("http://www.cbr.ru/currency_base/daily.aspx?date_req=" + System.DateTime.Now.Day + "." + System.DateTime.Now.Month + "." + System.DateTime.Now.Year);
string temp = "";
string temp2 = "";
var table = document.DocumentNode.SelectNodes("//table");
if (table != null)
{
foreach (var table_element in table)
{
if (table_element.Attributes["class"] != null && table_element.Attributes["class"].Value == "CBRTBL")
{
if (table_element.ChildNodes != null)
{
foreach (var tr_element in table_element.ChildNodes)
{
if (tr_element.Name == "tr")
{
temp = "";
temp2 = "";
if (tr_element.ChildNodes != null)
{
foreach (var td_element in tr_element.ChildNodes)
{
if (td_element.Name == "td")
{
temp = td_element.InnerText;
temp = temp.Replace(" ", "");
temp2 += temp;
temp2 += ";";
}
}
}
MessageBox.Show(temp2);
}
}
}
}
}
}
}
}
}Решение задачи: «Парсинг веб-страницы с использованием Html Agility Pack»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using calc_konstr.ServiceReference1;
namespace calc_konstr
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void read_curs_Click(object sender, EventArgs e)
{
var client = new DailyInfoSoapClient();
var returned_date = client.GetLatestDateTime();
var returned_date_property = client.GetCursOnDate(DateTime.Now).ExtendedProperties["OnDate"];
MessageBox.Show(returned_date_property.ToString());
MessageBox.Show(returned_date.ToString());
MessageBox.Show(DateTime.Now.ToString());
foreach (DataRow item in client.GetCursOnDate(DateTime.Now).Tables[0].Rows)
{
MessageBox.Show(item["Vname"].ToString().Trim() + "\n" + item["Vnom"].ToString().Trim() + "\n" + item["Vcurs"].ToString().Trim() + "\n" + item["Vcode"].ToString().Trim() + "\n" + item["VchCode"].ToString().Trim());
}
}
}
}