1

我有一个 xml 文件,例如:

<config>
   <email  Host="201.0.0.0" From="mail@mail.com" Pass="xxx" Name="xxx"/>
   <gir    g1="Traditional" g2="mid Techn" g3="High Tech"/>
   <costs>
     <Pre-Incube    inscr="7000.00"  add="300.00"/>
     <Normal        inscr="1600.00"  inc="7000.00" add="500.00"/>
     <Urgent        inscr="1600.00"  inc="5000.00" add="500.00"/>
     <Estance       inscr="1600.00"  men="2500.00"/> 
     <Post          inscr="1600.00"  men="1500.00"/> 
   </costs>
</config>

要获取元素“gir”的属性,我会这样做

Dim doc As XmlDocument = New XmlDocument()
doc.Load(path)
Dim root As XmlNode = doc.DocumentElement         
Dim nodeGir As XmlNode = root.SelectSingleNode("/config/gir")
cboGir.Items.Add(nodeGir.Attributes.ItemOf("g1").InnerText)
cboGir.Items.Add(nodeGir.Attributes.ItemOf("g2").InnerText)
cboGir.Items.Add(nodeGir.Attributes.ItemOf("g3").InnerText)

但是如何获取“成本”下的子元素的名称:

Pre-Incube, Normal, Urgent, Estance, Post  
4

2 回答 2

3

改编自此MSDN 页面

Dim costs As XmlNode = root.SelectSingleNode("/config/costs")

Dim i As Integer 
For i = 0 To costs.ChildNodes.Count - 1
    cboGir.Items.Add(costs.ChildNodes[i].Name)
Next i

或者可能更容易(来自这个 MSDN 页面):

Dim costs As XmlNodeList = root.SelectNodes("/config/costs/*")
For Each book In costs      
    cboGir.Items.Add(book.Name)
Next 

Name是XmlNode上的一个属性- 文档是您的朋友。

于 2012-12-22T22:21:08.137 回答
2

要获取所有成本子元素的名称,您可以执行以下操作:

For Each node As XmlNode In doc.SelectNodes("/config/costs/*")
    cboGir.Items.Add(node.Name)
Next
于 2012-12-23T00:53:52.933 回答