ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 523|ตอบกลับ: 0

[VB.NET] XML กับอัตราการแลกเปลี่ยนเงินตราเทียบกับสกุลเงินยูโร

[คัดลอกลิงก์]

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5971



โค้ดชุดนี้เป็นการคำนวณอัตราการแลกเปลี่ยนเงินตราเทียบกับสกุลเงินยูโรจาก European Central Bank (ECB) โค้ดจะทำการแยกแยะข้อมูล (Parser) ออกจากเอกสาร XML แล้วทำการจับคู่ (Pair) สกุลเงินกับอัตราแลกเปลี่ยน (2 Attribute) เพื่อนำไปคำนวณหาอัตราแลกเปลี่ยนระหว่างสกุลเงิน เช่น US Dollar มาเป็น Thai Baht ...

มาดูโค้ดกันเถอะ ...
  1. Public Class frmExchangeCurrency

  2.     '// Match the currency with the rate.
  3.     '// From XML (eXtensible Markup Language)
  4.     '// <Cube currency="USD" rate="1.0963"/>
  5.     Private ReadOnly ExchangeRate As Dictionary(Of String, Decimal) = New Dictionary(Of String, Decimal)()

  6.     Private Sub frmExchangeCurrency_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  7.         With Amount
  8.             .DecimalPlaces = 2
  9.             .ThousandsSeparator = True
  10.         End With
  11.         With cmbFrom
  12.             .IntegralHeight = False
  13.             .MaxDropDownItems = 10
  14.         End With
  15.         With cmbTo
  16.             .IntegralHeight = False
  17.             .MaxDropDownItems = 10
  18.         End With
  19.         lblUpDate.Text = ""
  20.         lblExchange.Text = ""
  21.     End Sub

  22.     Private Sub btnParser_Click(sender As System.Object, e As System.EventArgs) Handles btnParser.Click
  23.         lblExchange.Text = ""
  24.         Call InitialListView()
  25.         Call LoadCurrency()
  26.     End Sub

  27.     '// Initialize ListView Control
  28.     Sub InitialListView()
  29.         ExchangeRate.Clear()
  30.         With lvwRate
  31.             .Clear()
  32.             .View = View.Details
  33.             .GridLines = True
  34.             .FullRowSelect = True
  35.             .HideSelection = False
  36.             .MultiSelect = False
  37.             .Columns.Add("Currency", lvwRate.Width \ 2)
  38.             .Columns.Add("Rate", lvwRate.Width \ 2 - 20)
  39.         End With
  40.     End Sub

  41.     '// XML Parser.
  42.     Private Sub LoadCurrency()
  43.         '// Sample XML Format.
  44.         '<Cube>
  45.         '<Cube time="2023-04-10">
  46.         '<Cube currency="USD" rate="1.0963"/>
  47.         '<Cube currency="JPY" rate="117.66"/>
  48.         '<Cube currency="BGN" rate="1.9558"/>
  49.         '<Cube currency="CZK" rate="26.864"/>
  50.         '<Cube currency="DKK" rate="7.4623"/>
  51.         '<Cube currency="GBP" rate="0.87253"/>
  52.         '<Cube currency="HUF" rate="350.70"/>
  53.         '<Cube currency="PLN" rate="4.5473"/>
  54.         '<Cube currency="RON" rate="4.8343"/>
  55.         '<Cube currency="SEK" rate="10.9158"/>
  56.         '<Cube currency="CHF" rate="1.0543"/>
  57.         '<Cube currency="ISK" rate="155.90"/>
  58.         '<Cube currency="NOK" rate="11.3118"/>
  59.         '<Cube currency="HRK" rate="7.6130"/>
  60.         '<Cube currency="RUB" rate="80.3923"/>
  61.         '<Cube currency="TRY" rate="7.4472"/>
  62.         '<Cube currency="AUD" rate="1.7139"/>
  63.         '<Cube currency="BRL" rate="5.6585"/>
  64.         '<Cube currency="CAD" rate="1.5257"/>
  65.         '<Cube currency="CNY" rate="7.7366"/>
  66.         '<Cube currency="HKD" rate="8.4975"/>
  67.         '<Cube currency="IDR" rate="17229.09"/>
  68.         '<Cube currency="ILS" rate="3.9280"/>
  69.         '<Cube currency="INR" rate="83.4940"/>
  70.         '<Cube currency="KRW" rate="1332.53"/>
  71.         '<Cube currency="MXN" rate="25.8485"/>
  72.         '<Cube currency="MYR" rate="4.7508"/>
  73.         '<Cube currency="NZD" rate="1.8008"/>
  74.         '<Cube currency="PHP" rate="55.453"/>
  75.         '<Cube currency="SGD" rate="1.5521"/>
  76.         '<Cube currency="THB" rate="35.860"/>
  77.         '<Cube currency="ZAR" rate="20.0255"/>
  78.         '</Cube>
  79.         '</Cube>
  80.         Try
  81.             '// Provide Security in VS2010 and .Net Framework lower than 4.5
  82.             System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, System.Net.SecurityProtocolType)
  83.             Dim xmlDoc As New XmlDocument()
  84.             xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")
  85.             '// Update
  86.             '<Cube time="2023-04-21">
  87.             lblUpDate.Text = "Rates Updated: " & xmlDoc.DocumentElement.ChildNodes(2).ChildNodes(0).Attributes("time").Value.ToString
  88.             For Each node As XmlNode In xmlDoc.DocumentElement.ChildNodes(2).ChildNodes(0).ChildNodes
  89.                 '// Match currency and exchange rates.
  90.                 '// <Cube currency="USD" rate="1.0963"/>
  91.                 ExchangeRate.Add(node.Attributes("currency").Value, Decimal.Parse(node.Attributes("rate").Value))
  92.                 Dim LV As ListViewItem
  93.                 LV = lvwRate.Items.Add(node.Attributes("currency").Value)   '// Create main node.
  94.                 LV.SubItems.Add(node.Attributes("rate").Value)
  95.                 '// Add to ComboBox.
  96.                 cmbFrom.Items.Add(node.Attributes("currency").Value)
  97.                 cmbTo.Items.Add(node.Attributes("currency").Value)
  98.             Next
  99.             '// Sorting ListView from A to Z (Ascending)
  100.             Me.lvwRate.Sorting = SortOrder.Ascending
  101.             cmbFrom.SelectedIndex = 0
  102.             cmbTo.SelectedIndex = 0

  103.         Catch ex As Exception
  104.             MessageBox.Show(ex.Message, "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  105.         End Try
  106.     End Sub

  107.     Private Sub btnExchange_Click(sender As System.Object, e As System.EventArgs) Handles btnExchange.Click
  108.         If cmbFrom.Items.Count = 0 AndAlso cmbTo.Items.Count = 0 Then Return
  109.         '// Get pair value from ExchangeRate.
  110.         Dim ExchangeCurrency As Double = (Amount.Value / ExchangeRate(cmbFrom.Text)) * ExchangeRate(cmbTo.Text)
  111.         lblExchange.Text = Amount.Value & " " & cmbFrom.Text & " = " & ExchangeCurrency.ToString("#,##0.00") & " " & cmbTo.Text
  112.     End Sub

  113.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  114.         Me.Close()
  115.     End Sub

  116.     Private Sub frmExchangeCurrency_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  117.         Me.Dispose()
  118.         GC.SuppressFinalize(Me)
  119.         Application.Exit()
  120.     End Sub

  121. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...



ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-3-29 03:45 , Processed in 0.148831 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้