我有以下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>
在加载userform1时,我希望combobox1填充元素中的所有“id”属性。我有以下代码根据给定元素的内容填充它,但在这种情况下,这不是我需要的。感谢。
Dim doc As XDocument = XDocument.Load("path to xml")
Me.ComboBox1.DataSource = (From element In doc.Descendants("model") Select element.Value).ToList()
答案 0 :(得分:2)
试试这个:
Me.ComboBox1.DataSource =
(From element In doc.Root.Elements("device")
Select element.Attribute("id").Value).ToList()
答案 1 :(得分:1)
更广泛和类似于您的解决方案将是这样的:
Me.ComboBox1.DataSource =(from element in doc.Descendants("device")
select element.Attribute("id").Value).ToList();