Witam. Znalazłem w necie coś takiego.
http://zchpit.blogspot.com/2012/10/pobieranie-aktualnego-kursu-walut-z-nbp.html
Ale chciałem sobie to przerobić, żeby zamiast do konsoli wyświetlało mi to w listboxie.
jak na razie mam to:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string url = @"http://www.nbp.pl/kursy/xml/LastA.xml";
List<Currency> currencyList = ImportData(url);
foreach (Currency currency in currencyList)
{
}
}
private static List<Currency> ImportData(string url)
{
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("iso-8859-2");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
string readedData = readStream.ReadToEnd();
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(readedData);
XmlNodeList currencyListXml = xmlDoc.GetElementsByTagName(Constraints.POSITION);
var date = xmlDoc.GetElementsByTagName(Constraints.PUBLICATION_DATE);
DateTime currencyDate = Convert.ToDateTime(date[0].FirstChild.Value);
List<Currency> currencyList = new List<Currency>();
foreach (XmlNode currencyXml in currencyListXml)
{
//XmlNodeList nodes = currencyXml.ChildNodes;
Currency currency = new Currency();
currency.CurrencyName = currencyXml.ChildNodes[0].FirstChild.Value;
currency.Converter = currencyXml.ChildNodes[1].FirstChild.Value;
currency.CurrencyCode = currencyXml.ChildNodes[2].FirstChild.Value;
currency.CurrencyRate = Convert.ToDouble(currencyXml.ChildNodes[3].FirstChild.Value);
currency.CurrencyDate = currencyDate;
currencyList.Add(currency);
}
return currencyList;
}
class Currency
{
public string CurrencyName { get; set; }
public string Converter { get; set; }
public string CurrencyCode { get; set; }
public double CurrencyRate { get; set; }
public DateTime CurrencyDate { get; set; }
public Currency() { }
}
class Constraints
{
public static readonly string PUBLICATION_DATE = "data_publikacji";
public static readonly string POSITION = "pozycja";
public static readonly string CURRENCY_NAME = "nazwa_waluty";
public static readonly string CONVERETER = "przelicznik";
public static readonly string CURRENCY_CODE = "kod_waluty";
public static readonly string CURRENCY_RATE = "kurs_sredni";
}
}
Proszę o podpowiedz
Z góry dziękuję :)