如何在组合框中从XML获取值? [英] How to get a value from XML in a combo box ?
本文介绍了如何在组合框中从XML获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version="1.0"?>
<RISK>
<RISKITEM NAME="A576125" DESC="" DESCID="" PKEY="" YN="" RANGE="1" DATE="20170415082628.176000"/>
<RISKITEM NAME="A562512" DESC="" DESCID="" PKEY="" YN="" RANGE="1" DATE="20170415082628.176000"/>
<RISKITEM NAME="A551154" DESC="" DESCID="" PKEY="" YN="" RANGE="1" DATE="20170415082628.176000"/>
<RISKITEM NAME="A515648" DESC="" DESCID="" PKEY="" YN="" RANGE="1" DATE="20170415082628.176000"/>
<RISKITEM NAME="A548715" DESC="" DESCID="" PKEY="" YN="" RANGE="1" DATE="20170415082628.176000"/>
</RISK>
我试图将NAME提取到VB.NET的组合框中。尝试了几个例子,但没有奏效。请帮助,谢谢。
我有谷歌并尝试过互联网上的几个例子,但没有任何效果。任何heolp都会非常高兴。
我尝试过:
I am trying to fetch the "NAME" into a combo box in VB.NET. Tried several examples, but not working. Kindly help, thank you.
I have google and tried several examples from the internet, but nothing has worked. any heolp would be greatly apprecaited.
What I have tried:
<pre>Dim RA_File As String = "C:\Risk.xml"
cbxList.Items.Clear()
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(RA_File)
Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/RISK/RISKITEM")
Try
For Each node As XmlNode In nodes
cbxList.Items.Add(node.SelectSingleNode("NAME").InnerText)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
推荐答案
应该是:
Should be:
For Each node As XmlNode In nodes
Dim name = node["NAME"].InnerText
cbxList.Items.Add(Name)
Next
尝试下面的代码片段,它将解决您的问题 -
Try with below code snippet it will resolve your issue -
Dim RA_File As String = "E:\XMLFile1.xml"
ComboBox1.Items.Clear()
Dim xmlDoc As XDocument = XDocument.Load(RA_File)
Try
For Each node As XElement In xmlDoc.Root.Descendants("RISKITEM")
ComboBox1.Items.Add(node.Attribute("NAME").Value)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
这篇关于如何在组合框中从XML获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文