我正在尝试读取 XML 文件以便将数据集成到 Windows Phone 应用程序中。
我关注了其他一些主题,但我无法让它发挥作用(我觉得我差不多到了这一点,但仍然缺少一些东西)。
我试图阅读的 XML 是:
<?xml version="1.0" encoding="utf-8"?>
<items>
<item value="0">status</item>
<item value="210">online</item>
<item value="22h 49m 49s">uptime</item>
<item value="90">latency</item>
<item value="423">maxplayers_ever</item>
<item value="263">maxplayers_week</item>
<item value="252">maxplayers</item>
</items>
它包含游戏服务器的信息。
我正在从 URL 读取它,这就是我使用的代码:
public class Item
{
public string Name { get; set; }
public string Value { get; set; }
}
private void LoadXMLFile()
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += HttpsCompleted;
wc.DownloadStringAsync(new Uri("https://www.forgottenlands.eu/data.xml.php"));
}
private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
this.Items.Add(new ItemViewModel() { LineOne = "TEST I REACH HTTPS" });
XDocument statusinfo = XDocument.Parse(e.Result, LoadOptions.None);
List<Item> items =
(from node in statusinfo.Elements("Item")
select new Item
{
Name = (string)node.Value,
Value = (string)node.Attribute("Value")
}).ToList();
foreach (var item in items)
this.Items.Add(new ItemViewModel() { LineOne = item.Name + " " + item.Value });
this.IsDataLoaded = true;
}
}
public void LoadData()
{
// Sample data; replace with real data
// the xml file contains your provided xml code
LoadXMLFile();
}
似乎我正确地进入了httpscompleted
函数,但我没有正确地获得 XML 数据。