Переодически возникает необходимость получения текущего курса валют (относительно рубля). Задача простая, но все-таки вот готовый модуль на VB.NET.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
Public Class Valut Private c_ID As String Private c_Code As String Private c_CharCode As String Private c_Nominal As Double Private c_Name As String Private c_Value As Double Private Sub New(el As Xml.XmlElement) c_ID = el.GetAttribute("ID") c_Code = el("NumCode").InnerText c_CharCode = el("CharCode").InnerText c_Nominal = CDbl(el("Nominal").InnerText) c_Name = el("Name").InnerText c_Value = CDbl(el("Value").InnerText) End Sub Public ReadOnly Property ID As String Get Return c_ID End Get End Property Public ReadOnly Property Code As String Get Return c_Code End Get End Property Public ReadOnly Property CharCode As String Get Return c_CharCode End Get End Property Public ReadOnly Property Name As String Get Return c_Name End Get End Property Public ReadOnly Property Nominal As Double Get Return c_Nominal End Get End Property Public ReadOnly Property Value As Double Get Return c_Value End Get End Property Public ReadOnly Property NominaledValue As Double Get Return c_Value / c_Nominal End Get End Property Public Overrides Function ToString() As String Return c_Name & " (" & c_CharCode & ")" End Function Private Shared c_cache As List(Of Valut) Private Shared c_cachedate As Date = Nothing ''' <summary> ''' Время кеширования данных в минутах. По умолчанию - сутки. ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public Shared Property CacheTimeout As Integer = 60 * 24 'Сутки Public Shared Function GetValuts() As List(Of Valut) If c_cache IsNot Nothing Then If Now <= c_cachedate Then Return c_cache End If End If Dim doc As New Xml.XmlDocument Try Dim wc As New Net.WebClient Dim xml As String = wc.DownloadString("http://www.cbr.ru/scripts/XML_daily.asp?") doc.LoadXml(xml) Catch ex As Exception Throw New Exception("Не удалось загрузить справочник курсов валют. Проверьте свое Интернет подключение (www.cbr.ru).", ex) End Try Dim res As New List(Of Valut) For Each el As Xml.XmlElement In doc("ValCurs").ChildNodes res.Add(New Valut(el)) Next c_cache = res c_cachedate = Now.AddMinutes(CacheTimeout) Return res End Function Public Shared Function GetByID(id As String) As Valut For Each v As Valut In GetValuts() If v.ID = id Then Return v End If Next Return Nothing End Function Public Shared Function GetByCode(code As String) As Valut For Each v As Valut In GetValuts() If v.Code = code Then Return v End If Next Return Nothing End Function Public Shared Function GetByCharCode(code As String) As Valut For Each v As Valut In GetValuts() If v.CharCode = code Then Return v End If Next Return Nothing End Function End Class |
Класс кеширует загруженные курсы, по умолчанию, на сутки. Т.е. при повторном вызове метода GetValuts() повторного запроса в Интернет не будет.
Использовать так:
1 2 3 4 5 6 7 8 9 |
Sub Test() Dim usd As Valut = Valut.GetByID("R01235") usd = Valut.GetByCode("840") usd = Valut.GetByCharCode("USD") For Each v As Valut In Valut.GetValuts Debug.Print(v.ToString) Next End Sub |