我的XML文件看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<root>
<mob name="mob1" lvl="5" hp="30" mp="25"/>
<mob name="mob2" lvl="6" hp="50" mp="55"/>
<mob name="mob3" lvl="9" hp="80" mp="85"/>
<mob name="mob4" lvl="11" hp="130" mp="125"/>
</root>
我是编程和处理程序的新手,我可以列出并跟踪数据。 我想要做的是有一个组合框,其中填充了“name”属性中的条目,当您在组合框中选择条目时,它将从同一节点拉出Lvl,hp,mp属性并将它们放入一些要使用和显示的变量。
我工作的方式是使用像这样的XML文件
<?xml version="1.0" encoding="utf-8" ?>
<root>
<moblist list="mob1,mob2,mob3,mob4"/>
<mob0 lvl="5" hp="30" mp="25"/>
<mob1 lvl="6" hp="50" mp="55"/>
<mob2 lvl="9" hp="80" mp="85"/>
<mob3 lvl="11" hp="130" mp="125"/>
</root>
将moblist节点列表属性转换为列表,并使用它来填充组合框。 然后做了这样的事情:
newindex = cmb_mobs.SelectedIndex
index = "/root/mob" & newindex
Dim doc As XmlDocument = New XmlDocument()
doc.Load("C:/test.xml")
Dim root As XmlNode = doc.DocumentElement
Dim mobs As XmlNode
mobs = root.SelectSingleNode(index)
Dim shw_lvl As String = mobs.Attributes.ItemOf("lvl").InnerText
Dim shw_hp As String = mobs.Attributes.ItemOf("hp").InnerText
Dim shw_mp As String = mobs.Attributes.ItemOf("mp").InnerText
每当组合框改变选定的索引时,我就会这样做。
问题1:有没有更好的方法来实现这些结果?如果可以做得更好,我愿意改变xml结构。
答案 0 :(得分:2)
您可以使用xml序列化。
使用此xml文件
<?xml version="1.0" encoding="utf-8" ?>
<root>
<mob name="mob1" lvl="5" hp="30" mp="25"/>
<mob name="mob2" lvl="6" hp="50" mp="55"/>
<mob name="mob3" lvl="9" hp="80" mp="85"/>
<mob name="mob4" lvl="11" hp="130" mp="125"/>
</root>
在VB
中定义这样的xml模型Imports System.Xml.Serialization
Imports System.IO
<XmlRoot("root")>
Public Class XmlModel
<XmlElement("mob")>
Public Property Mobs As List(Of XmlMob)
End Class
<Serializable>
Public Class XmlMob
<XmlAttribute("name")>
Public Property Name As String
<XmlAttribute("lvl")>
Public Property Level As Integer
<XmlAttribute("hp")>
Public Property HitPoints As Integer
<XmlAttribute("mp")>
Public Property MagicPoints As Integer
End Class
然后将xml文件反序列化为XmlModel
类
Private xmlMobs As XmlModel
Private Sub LoadXmlFile()
Dim s As New XmlSerializer(GetType(XmlModel))
Using sr As New StreamReader("c:/test.xml")
xmlMobs = s.Deserialize(sr)
End Using
End Sub
现在你有了一个变量xmlMobs
,它可以存放你所有的怪物信息。然后,您可以修改此变量(更改名称,统计信息等),并序列化回文件。
Private Sub SaveXmlFile()
Dim s As New XmlSerializer(GetType(XmlModel))
Using sw As New StreamWriter("c:/test.xml")
s.Serialize(sw, xmlMobs)
End Using
End Sub
答案 1 :(得分:1)
您可以将XML缓存在内存中,如下所示:
Dim xml = <?xml version="1.0" encoding="utf-8"?>
<root>
<mob name="mob1" lvl="5" hp="30" mp="25"/>
<mob name="mob2" lvl="6" hp="50" mp="55"/>
<mob name="mob3" lvl="9" hp="80" mp="85"/>
<mob name="mob4" lvl="11" hp="130" mp="125"/>
</root>
Dim mobs As New List(Of Mob)
For Each e As XElement In xml.Root.Elements
Dim m As New Mob
m.name = e.Attribute("name").Value
m.lvl = e.Attribute("lvl").Value
m.hp = e.Attribute("hp").Value
m.mp = e.Attribute("mp").Value
mobs.Add(m)
Next
假设以下类Mob:
Class Mob
Public Property name As String
Public Property lvl As String
Public Property hp As String
Public Property mp As String
Public Overrides Function ToString() As String
Return Me.name
End Function
End Class
然后使用数据绑定来填充组合框:
comboBox1.DataSource = mobs
然后在SelectedIndexChanged事件中,从选定的Mob中提取并显示您需要的数据:
Dim m As Mob = DirectCast(comboBox1.SelectedItem, Mob)
Dim shw_lvl As String = m.lvl
Dim shw_hp As String = m.hp
Dim shw_mp As String = m.mp