我只是無法抗拒這一點,因爲正則表達式對XML僅僅是不是一個好主意。
您對樣品XML鏈接還跟提供了一個模式:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="players">
<xs:complexType>
<xs:sequence>
<xs:element name="player" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" use="required" type="xs:integer"/>
<xs:attribute name="name" use="required" type="xs:string"/>
<xs:attribute name="status" use="optional">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(a|[vIibo]+)"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="alliance" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="timestamp" type="xs:integer"/>
<xs:attribute name="serverId" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
這將產生以下兩個類(我們不關心在這種情況下限制):
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Collections.Specialized
Imports System.Xml.Serialization
Imports System.Diagnostics
Imports System.Collections.Generic
Imports System.Linq
<XmlType(AnonymousType:=True, TypeName:="players"), XmlRoot(ElementName:="players")>
Public Class PlayerList
<XmlElement("player", Form:=XmlSchemaForm.Unqualified, ElementName:="player")>
Public Property Players() As New List(Of Player)
<XmlAttribute(AttributeName:="timestamp"), DefaultValue(0)>
Public Property Timestamp() As Integer
<XmlAttribute(AttributeName:="serverId"), DefaultValue("")>
Public Property ServerId() As String
Public Function Find(PlayerName As String) As Player
Return Players.FirstOrDefault(Function(p) p.Name = PlayerName)
End Function
End Class
<XmlType(AnonymousType:=True, TypeName:="player"), XmlRoot("player")>
Public Class Player
<XmlAttribute(AttributeName:="id"), DefaultValue(0)>
Public Property Id() As Integer
<XmlAttribute(AttributeName:="name"), DefaultValue("")>
Public Property Name() As String
<XmlAttribute(AttributeName:="status"), DefaultValue("")>
Public Property Status() As String
<XmlAttribute(AttributeName:="alliance"), DefaultValue("")>
Public Property Alliance() As String
End Class
我已經添加在PlayerList類Find
功能,爲您的按鈕處理程序調用:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Link As String = "https://s25-pt.ogame.gameforge.com/api/players.xml"
Dim MyPlayers As PlayerList = Nothing
With New WebClient
.Headers.Add("user-agent", "Mozilla/5.0 (Windows; U; Windows NT 5.0; es-ES; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3")
MyPlayers = Deserialize(.DownloadString(Link), GetType(PlayerList))
.Dispose()
End With
Dim MyPlayer As Player = MyPlayers.Find(ListBox1.Text)
If MyPlayer IsNot Nothing Then
Debug.Print("Player ID: {0}", MyPlayer.Id)
Debug.Print("Player Name: {0}", MyPlayer.Name)
Debug.Print("Player Status: {0}", MyPlayer.Status)
Debug.Print("Player Alliance: {0}", MyPlayer.Alliance)
Else
Debug.Print("Not Found")
End If
End Sub
Private Function Deserialize(XMLString As String, ObjectType As Type) As Object
Return New XmlSerializer(ObjectType).Deserialize(New MemoryStream(Encoding.UTF8.GetBytes(XMLString)))
End Function
使用Fantasma2
進行測試我得到以下輸出:
Player ID: 100110
Player Name: Fantasma2
Player Status: vI
Player Alliance: 4762
這很好用。感謝您的幫助和詳細的解釋。 :) –