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

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

[VB.NET] XML กับอัตราการแลกเปลี่ยนเงินตรา จากบริการฟรีของ Yahoo Finance

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

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6072


โค้ดเบาๆเล็กๆนี้จะแสดงการอ่านค่าจาก finance.yahoo.com ซึ่งจะถูกส่งค่ากลับมาเป็น XML (eXtensible Markup Language) เพื่อแสดงอัตราการแลกเปลี่ยนเงินตรา โดยยึดสกุลหลักคือ US Dollar มาเป็นสกุลเงินต่างๆ ...
  1. https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=xml
คัดลอกไปที่คลิปบอร์ด
URL ลิ้งค์ที่ทำการอ่านค่า XML
  1. <resource classname="Quote">
  2. <field name="name">USD/KRW</field>
  3. <field name="price">1125.170044</field>
  4. <field name="symbol">KRW=X</field>
  5. <field name="ts">1507932779</field>
  6. <field name="type">currency</field>
  7. <field name="utctime">2017-10-13T22:12:59+0000</field>
  8. <field name="volume">0</field>
  9. </resource>
คัดลอกไปที่คลิปบอร์ด
ตัวอย่างของรูปแบบ XML ซึ่งจะแสดงผลใน Web Browser มีหลักการง่ายๆโดยเราจะทำการอ่านค่า Element หลักคือ "Quote" จากนั้นเลือก Attribute ที่มีชื่อว่า name และ price

มาดูโค้ดกันเถอะ ...
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Purpose: Get currency exchange rates from finance.yahoo.com
  8. ' / Microsoft Visual Basic .NET (2010)
  9. ' /
  10. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  11. ' / You can modify and/or distribute without to inform the developer.
  12. ' / --------------------------------------------------------------------------------
  13. Imports System.Xml

  14. Public Class frmExchangeCurrency

  15.     Private Sub btnParser_Click(sender As System.Object, e As System.EventArgs) Handles btnParser.Click
  16.         Dim url As String = "https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=xml"
  17.         '// Sample XML Format.
  18.         '<resource classname="Quote">
  19.         '<field name="name">USD/VND</field>
  20.         '<field name="price">22714.000000</field>
  21.         '<field name="symbol">VND=X</field>
  22.         '<field name="ts">1507326331</field>
  23.         '<field name="type">currency</field>
  24.         '<field name="utctime">2017-10-06T21:45:31+0000</field>
  25.         '<field name="volume">0</field>
  26.         '</resource>

  27.         Try
  28.             ' Load the data.
  29.             Dim doc As New XmlDocument()
  30.             doc.Load(url)
  31.             '// Process the resource nodes.
  32.             Dim xRoot As XmlNode = doc.DocumentElement
  33.             '// Get the element.
  34.             Dim xElement As String = "descendant::resource[@classname='Quote']"
  35.             For Each node As XmlNode In xRoot.SelectNodes(xElement)
  36.                 '// Get the attribute.
  37.                 Dim name As String = node.SelectSingleNode("field[@name='name']").InnerText
  38.                 Dim price As String = node.SelectSingleNode("field[@name='price']").InnerText
  39.                 '// Read the information only with the / sign in the 4th order. (For example--> USD/VND)
  40.                 If InStr(name, "/") = 4 Then
  41.                     Dim LV As ListViewItem
  42.                     LV = lvwPrices.Items.Add(name)  '// Create main node.
  43.                     LV.SubItems.Add(price)
  44.                 End If
  45.             Next
  46.             '// Sorting from A to Z
  47.             Me.lvwPrices.Sorting = SortOrder.Ascending

  48.         Catch ex As Exception
  49.             MessageBox.Show(ex.Message, "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  50.         End Try

  51.     End Sub

  52.     Private Sub frmExchangeCurrency_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  53.         ' Initialize ListView Control
  54.         With lvwPrices
  55.             .View = View.Details
  56.             .GridLines = True
  57.             .FullRowSelect = True
  58.             .HideSelection = False
  59.             .MultiSelect = False
  60.             .Columns.Add("Name", lvwPrices.Width \ 2)
  61.             .Columns.Add("Price", lvwPrices.Width \ 2 - 20)
  62.         End With
  63.     End Sub

  64.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  65.         Me.Close()
  66.     End Sub

  67.     Private Sub frmExchangeCurrency_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  68.         Me.Dispose()
  69.         Application.Exit()
  70.     End Sub

  71. End Class
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่

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

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

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

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

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

GMT+7, 2024-5-5 16:56 , Processed in 0.193499 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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