我有这个xml文件:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<root>
<radio>
<title>The Autobiography of Benjamin Franklin</title>
<url>https://www.google.com</url>
</radio>
<radio>
<title>The Autobiography of n Franklin</title>
<url>https://www.facebook.com</url>
</radio>
<radio>
<title>The Autobiography of Benjaminin</title>
<url>https://www.yahoo.com</url>
</radio>
</root>
我通过以下代码在组合框中显示了标题标记:
Dim doc As New XmlDocument()
doc.Load("books.xml")
Dim elemList As XmlNodeList = doc.GetElementsByTagName("title")
Dim i As Integer
For i = 0 To elemList.Count - 1
ComboBox1.Items.Add(elemList(i).InnerXml)
Next i
所以我想当用户点击组合框中的标题时,网页浏览器导航到与xml文件中的标题链接的网址。
答案 0 :(得分:0)
C#
将xml加载到匿名类型列表。将它绑定到组合框。
var xmlDoc = XElement.Load("books.xml");
var nodes = xmlDoc.Elements("radio")
.Select(elem => new
{
Title = elem.Element("title").Value,
Url = elem.Element("url").Value
})
.ToList();
comboBox.DisplayMember = "Title";
comboBox.ValueMember = "Url";
comboBox.DataSource = nodes;
导航至网址。
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser.Navigate(comboBox.SelectedValue.ToString());
}