2013-12-18 51 views
0

首先我的代碼如何Xattributes獲取值

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查詢,所以我可以從列表視圖XAttribute元素顯示值,我有它的方式,現在的作品,但它僅在第一列顯示。它應顯示狀態列中的「在線」節點,碎片名稱列中的「名稱」,服務器類型列中的「類型」等。

有人可以給我建議嗎?

Screen

+0

你怎麼你在ListVi中獲得這些列標題EW? –

+0

我在設計師做過他們。 – Michael27

回答

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); 

UPDATE
或移動這像這樣

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")); 
+0

我喜歡這兩種解決方案,都適合我,但Grundy's很容易閱讀和理解。我更喜歡可讀性,特別是如果它是方法形式。 乾杯和謝謝。 – Michael27

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屬性設置爲「詳細信息」。