[VB.NET] XML กับอัตราการแลกเปลี่ยนเงินตรา จากบริการฟรีของ Yahoo Finance
http://www.g2gnet.com/webboard/images/vbnet/YahooCurrency.pngโค้ดเบาๆเล็กๆนี้จะแสดงการอ่านค่าจาก finance.yahoo.com ซึ่งจะถูกส่งค่ากลับมาเป็น XML (eXtensible Markup Language) เพื่อแสดงอัตราการแลกเปลี่ยนเงินตรา โดยยึดสกุลหลักคือ US Dollar มาเป็นสกุลเงินต่างๆ ...
https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=xmlURL ลิ้งค์ที่ทำการอ่านค่า XML
<resource classname="Quote">
<field name="name">USD/KRW</field>
<field name="price">1125.170044</field>
<field name="symbol">KRW=X</field>
<field name="ts">1507932779</field>
<field name="type">currency</field>
<field name="utctime">2017-10-13T22:12:59+0000</field>
<field name="volume">0</field>
</resource>ตัวอย่างของรูปแบบ XML ซึ่งจะแสดงผลใน Web Browser มีหลักการง่ายๆโดยเราจะทำการอ่านค่า Element หลักคือ "Quote" จากนั้นเลือก Attribute ที่มีชื่อว่า name และ price
มาดูโค้ดกันเถอะ ...
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Get currency exchange rates from finance.yahoo.com
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Imports System.Xml
Public Class frmExchangeCurrency
Private Sub btnParser_Click(sender As System.Object, e As System.EventArgs) Handles btnParser.Click
Dim url As String = "https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=xml"
'// Sample XML Format.
'<resource classname="Quote">
'<field name="name">USD/VND</field>
'<field name="price">22714.000000</field>
'<field name="symbol">VND=X</field>
'<field name="ts">1507326331</field>
'<field name="type">currency</field>
'<field name="utctime">2017-10-06T21:45:31+0000</field>
'<field name="volume">0</field>
'</resource>
Try
' Load the data.
Dim doc As New XmlDocument()
doc.Load(url)
'// Process the resource nodes.
Dim xRoot As XmlNode = doc.DocumentElement
'// Get the element.
Dim xElement As String = "descendant::resource[@classname='Quote']"
For Each node As XmlNode In xRoot.SelectNodes(xElement)
'// Get the attribute.
Dim name As String = node.SelectSingleNode("field[@name='name']").InnerText
Dim price As String = node.SelectSingleNode("field[@name='price']").InnerText
'// Read the information only with the / sign in the 4th order. (For example--> USD/VND)
If InStr(name, "/") = 4 Then
Dim LV As ListViewItem
LV = lvwPrices.Items.Add(name)'// Create main node.
LV.SubItems.Add(price)
End If
Next
'// Sorting from A to Z
Me.lvwPrices.Sorting = SortOrder.Ascending
Catch ex As Exception
MessageBox.Show(ex.Message, "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub frmExchangeCurrency_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Initialize ListView Control
With lvwPrices
.View = View.Details
.GridLines = True
.FullRowSelect = True
.HideSelection = False
.MultiSelect = False
.Columns.Add("Name", lvwPrices.Width \ 2)
.Columns.Add("Price", lvwPrices.Width \ 2 - 20)
End With
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()
Application.Exit()
End Sub
End Classดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่
หน้า:
[1]