首先是我的代码
XDocument USshardStatus = XDocument.Load("http://status.riftgame.com/na-status.xml");
XDocument EuropeShardStatus = XDocument.Load("http://status.riftgame.com/eu-status.xml");
List<IEnumerable<XAttribute>> USRiftShard = new List<IEnumerable<XAttribute>>();
//Attributes contains values from "online" node, there is 7 elements
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("online"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("name"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("locked"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("population"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("queued"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("language"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("pvp"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("rp"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("recommend"));
USRiftShard.Add(USshardStatus.Descendants("shard").Attributes("initialCreationRestriction"));
List<IEnumerable<XAttribute>> EuropeRiftShard = new List<IEnumerable<XAttribute>();
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("online"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("name"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("locked"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("population"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("queued"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("language"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("pvp"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("rp"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("recommend"));
EuropeRiftShard.Add(EuropeShardStatus.Descendants("shard").Attributes("initialCreationRestriction"));
foreach (IEnumerable<XAttribute> statusUS in USRiftShard)
{
foreach (XAttribute xatrib in statusUS)
{
lvNorthAmericaShard.Items.Add(xatrib.Value);
}
}
foreach (IEnumerable<XAttribute> statusEU in EuropeRiftShard)
{
foreach (XAttribute xAttribute in statusEU)
{
lvEuropeShard.Items.Add(xAttribute.Value);
}
}
现在的问题是,我如何构建LINQ查询,以便我可以在listview中显示XAttribute元素的值,我现在的方式正常工作,但它只在第一列显示。它应该在状态列中显示“在线”节点,在分片名称列中显示“名称”,在“服务器类型”列中显示“类型”等。
有人可以给我建议吗?
答案 0 :(得分:2)
我认为你需要这样的东西
XDocument USshardStatus = XDocument.Load("http://status.riftgame.com/na-status.xml");
var items = from node in USshardStatus.Descendants("shard")
select new ListViewItem(new string[] {
//select attributes what you need
node.Attribute("online").Value,
node.Attribute("name").Value,
node.Attribute("locked").Value,
node.Attribute("population").Value,
node.Attribute("queued").Value,
})).ToArray();
lvNorthAmericaShard.Items.AddRange(items);
<强>更新强>
或者将其移动到这样的功能
private ListViewItem[] GetItems(string url, params string[] attrNames)
{
return (from node in XDocument.Load(url).Descendants("shard")
select new ListViewItem(
attrNames.Select(attr=>node.Attribute(attr).Value).ToArray()
)).ToArray();
}
并像
一样使用它lvNorthAmericaShard.Items.AddRange(GetItems("http://status.riftgame.com/na-status.xml",
"online",
"name",
"locked",
"population",
"queued",
"language"));
答案 1 :(得分:1)
你必须遍历所有元素,并将它们全部循环到listView
foreach (XElement element in USshardStatus.Descendants().
Where(x => x.Name.LocalName == "status").
Descendants().
Where(y => y.Name.LocalName == "shart"))
{
//Add items to your listviews.
string onlineValue = element.Elements().Where(x => x.Name.LocalName == "online").SingleOrDefault().Value;
string name = element.Elements().Where(x => x.Name.LocalName == "name").SingleOrDefault().Value;
//So on...
string[] row = {onlineValue, name};
listView.Items.Add(new ListViewItem(row));
}
这对我有用。确保ListView的View
属性设置为“详细信息”。