Click here to Skip to main content
15,357,293 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
<?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>


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:

VB
<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
Posted
Updated 19-May-22 13:16pm
Comments
Member 13051967 5-May-17 8:10am
   
It says string is a class type and cannot be used as an expression..

I tried the below.. but still no luck.. I am trying this in VB.

For Each node As XmlNode In nodes
'cbxList.Items.Add(node.SelectSingleNode("NAME").InnerText)
cbxList.Items.Add(node("NAME").InnerText)
Next

Should be:

For Each node As XmlNode In nodes
      Dim name = node["NAME"].InnerText
      cbxList.Items.Add(Name)
Next
   
v3
Try with below code snippet it will resolve your issue -


VB
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
   
Comments
Member 13051967 5-May-17 8:18am
   
No erros, but this one too did not work.
[no name] 5-May-17 8:20am
   
It working for me. Did you replace file name with yours in RA_File variable?
Member 13051967 5-May-17 8:35am
   
OOps. I moved the File was under a sub folder. Its working, I am sorry, thank you so much...
ComboBox1.Items.Add(node.InnerText)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900