简体   繁体   中英

How to get element names from an xml document in vb.net

I have a xml file like:

<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>

To get the attributes for the element "gir" I do

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)

But how do I get the names of the child elements under "costs":

Pre-Incube, Normal, Urgent, Estance, Post  

Adapted from this MSDN page :

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

Or probably easier (from this MSDN page ):

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

Name is a property on XmlNode - the docs are your friend.

To get the names of all the elements that are children of costs, you can do this:

For Each node As XmlNode In doc.SelectNodes("/config/costs/*")
    cboGir.Items.Add(node.Name)
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM