Pergunta

I have the following XML

<device id="DI-1561">
    <model>LG-F320L</model>
    <series>LG B2</series>
    <os>4.4.2</os>
    <location>Unk</location>
</device>
<device id="DI-1572">
    <model>SM-G900H</model>
    <series>GS5</series>
    <os>4.4.2</os>
    <location>Unk</location>
</device>

On loading userform1, I want combobox1 to populate with all of the "id" attributes from element. I have the following code to populate it based on the content of a given element, but that isn't what I need in this case. Thanks.

        Dim doc As XDocument = XDocument.Load("path to xml")
    Me.ComboBox1.DataSource = (From element In doc.Descendants("model") Select element.Value).ToList()
Foi útil?

Solução

Try this:

Me.ComboBox1.DataSource =
  (From element In doc.Root.Elements("device")
   Select element.Attribute("id").Value).ToList()

Outras dicas

More generalized and similar to your solution would be something like this:

     Me.ComboBox1.DataSource =(from element in doc.Descendants("device") 
                               select element.Attribute("id").Value).ToList();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top