我想从XML文件(https://xml.buienradar.nl/)获取数据并使用该数据填充组合框。我想要来自<weerstation regio=>
的每个区域
在组合框中显示。
我在UWP找不到任何适合我的东西
答案 0 :(得分:0)
您可以使用LinqToXML来读取数据,然后将其设置为ItemSource。
答案 1 :(得分:0)
您可以从以下代码段开始(请参阅操作here):
var xml = XDocument.Load("https://xml.buienradar.nl/");
var weerstationList = xml.Descendants("weerstation");
var weerstations = weerstationList.Select(w => new Item()
{
Id = w.Attribute("id").Value,
Name = w.Descendants("stationnaam").First().Value
});
以及以下助手类:
public class Item
{
public string Id { get; set; }
public string Name { get; set; }
}
然后,您可以将项目列表weerstations
设置为组合框的ItemSource
。
答案 2 :(得分:0)
最简单的方法是使用XElement
加载XML,然后通过它进行查询。
首先,您需要在XAML页面中ComboBox
:
<ComboBox x:Name="Stations" />
为Loaded
事件添加新的事件处理程序,并在代码中使用此代码加载XML并填充ComboBox:
private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
var requestUri = new Uri("https://xml.buienradar.nl/");
var httpClient = new HttpClient();
var httpResponse = await httpClient.GetAsync(requestUri);
httpResponse.EnsureSuccessStatusCode();
var httpResponseStream = await httpResponse.Content.ReadAsStreamAsync();
var root = XElement.Load(httpResponseStream);
var stations =
from element in root.Descendants("weerstation")
select element;
foreach (var station in stations)
{
Stations.Items?.Add(station.Attribute("id")?.Value);
}
}
这是link to source where I get idea。 还有link where you'll find documentation how to do simple HTTP GET request。