[VB.NET] XML กับอัตราการแลกเปลี่ยนเงินตราเทียบกับสกุลเงินยูโร
http://www.g2gsoft.com/webboard/images/VBNet/exchangecurrency.pngโค้ดชุดนี้เป็นการคำนวณอัตราการแลกเปลี่ยนเงินตราเทียบกับสกุลเงินยูโรจาก European Central Bank (ECB) โค้ดจะทำการแยกแยะข้อมูล (Parser) ออกจากเอกสาร XML แล้วทำการจับคู่ (Pair) สกุลเงินกับอัตราแลกเปลี่ยน (2 Attribute) เพื่อนำไปคำนวณหาอัตราแลกเปลี่ยนระหว่างสกุลเงิน เช่น US Dollar มาเป็น Thai Baht ...
มาดูโค้ดกันเถอะ ...
Public Class frmExchangeCurrency
'// Match the currency with the rate.
'// From XML (eXtensible Markup Language)
'// <Cube currency="USD" rate="1.0963"/>
Private ReadOnly ExchangeRate As Dictionary(Of String, Decimal) = New Dictionary(Of String, Decimal)()
Private Sub frmExchangeCurrency_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With Amount
.DecimalPlaces = 2
.ThousandsSeparator = True
End With
With cmbFrom
.IntegralHeight = False
.MaxDropDownItems = 10
End With
With cmbTo
.IntegralHeight = False
.MaxDropDownItems = 10
End With
lblUpDate.Text = ""
lblExchange.Text = ""
End Sub
Private Sub btnParser_Click(sender As System.Object, e As System.EventArgs) Handles btnParser.Click
lblExchange.Text = ""
Call InitialListView()
Call LoadCurrency()
End Sub
'// Initialize ListView Control
Sub InitialListView()
ExchangeRate.Clear()
With lvwRate
.Clear()
.View = View.Details
.GridLines = True
.FullRowSelect = True
.HideSelection = False
.MultiSelect = False
.Columns.Add("Currency", lvwRate.Width \ 2)
.Columns.Add("Rate", lvwRate.Width \ 2 - 20)
End With
End Sub
'// XML Parser.
Private Sub LoadCurrency()
'// Sample XML Format.
'<Cube>
'<Cube time="2023-04-10">
'<Cube currency="USD" rate="1.0963"/>
'<Cube currency="JPY" rate="117.66"/>
'<Cube currency="BGN" rate="1.9558"/>
'<Cube currency="CZK" rate="26.864"/>
'<Cube currency="DKK" rate="7.4623"/>
'<Cube currency="GBP" rate="0.87253"/>
'<Cube currency="HUF" rate="350.70"/>
'<Cube currency="PLN" rate="4.5473"/>
'<Cube currency="RON" rate="4.8343"/>
'<Cube currency="SEK" rate="10.9158"/>
'<Cube currency="CHF" rate="1.0543"/>
'<Cube currency="ISK" rate="155.90"/>
'<Cube currency="NOK" rate="11.3118"/>
'<Cube currency="HRK" rate="7.6130"/>
'<Cube currency="RUB" rate="80.3923"/>
'<Cube currency="TRY" rate="7.4472"/>
'<Cube currency="AUD" rate="1.7139"/>
'<Cube currency="BRL" rate="5.6585"/>
'<Cube currency="CAD" rate="1.5257"/>
'<Cube currency="CNY" rate="7.7366"/>
'<Cube currency="HKD" rate="8.4975"/>
'<Cube currency="IDR" rate="17229.09"/>
'<Cube currency="ILS" rate="3.9280"/>
'<Cube currency="INR" rate="83.4940"/>
'<Cube currency="KRW" rate="1332.53"/>
'<Cube currency="MXN" rate="25.8485"/>
'<Cube currency="MYR" rate="4.7508"/>
'<Cube currency="NZD" rate="1.8008"/>
'<Cube currency="PHP" rate="55.453"/>
'<Cube currency="SGD" rate="1.5521"/>
'<Cube currency="THB" rate="35.860"/>
'<Cube currency="ZAR" rate="20.0255"/>
'</Cube>
'</Cube>
Try
'// Provide Security in VS2010 and .Net Framework lower than 4.5
System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, System.Net.SecurityProtocolType)
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")
'// Update
'<Cube time="2023-04-21">
lblUpDate.Text = "Rates Updated: " & xmlDoc.DocumentElement.ChildNodes(2).ChildNodes(0).Attributes("time").Value.ToString
For Each node As XmlNode In xmlDoc.DocumentElement.ChildNodes(2).ChildNodes(0).ChildNodes
'// Match currency and exchange rates.
'// <Cube currency="USD" rate="1.0963"/>
ExchangeRate.Add(node.Attributes("currency").Value, Decimal.Parse(node.Attributes("rate").Value))
Dim LV As ListViewItem
LV = lvwRate.Items.Add(node.Attributes("currency").Value) '// Create main node.
LV.SubItems.Add(node.Attributes("rate").Value)
'// Add to ComboBox.
cmbFrom.Items.Add(node.Attributes("currency").Value)
cmbTo.Items.Add(node.Attributes("currency").Value)
Next
'// Sorting ListView from A to Z (Ascending)
Me.lvwRate.Sorting = SortOrder.Ascending
cmbFrom.SelectedIndex = 0
cmbTo.SelectedIndex = 0
Catch ex As Exception
MessageBox.Show(ex.Message, "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub btnExchange_Click(sender As System.Object, e As System.EventArgs) Handles btnExchange.Click
If cmbFrom.Items.Count = 0 AndAlso cmbTo.Items.Count = 0 Then Return
'// Get pair value from ExchangeRate.
Dim ExchangeCurrency As Double = (Amount.Value / ExchangeRate(cmbFrom.Text)) * ExchangeRate(cmbTo.Text)
lblExchange.Text = Amount.Value & " " & cmbFrom.Text & " = " & ExchangeCurrency.ToString("#,##0.00") & " " & cmbTo.Text
End Sub
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmExchangeCurrency_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
GC.SuppressFinalize(Me)
Application.Exit()
End Sub
End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
หน้า:
[1]