C# 如何获取属性中的值

C# 如何获取属性中的值,c#,linq,C#,Linq,首先是我的代码 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&

首先是我的代码

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);
    }
}
XDocument USshardStatus=XDocument.Load(“http://status.riftgame.com/na-status.xml");
XDocument EuropeShardStatus=XDocument.Load(“http://status.riftgame.com/eu-status.xml");
List USRiftShard=新列表();
//属性包含来自“online”节点的值,共有7个元素
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“online”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“name”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“locked”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“population”);
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“queued”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“language”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“pvp”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“rp”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“推荐”));
USRiftShard.Add(USshardStatus.substands(“shard”).Attributes(“initialCreationRestriction”);

List-EuropeRiftShard=new-List您必须遍历所有元素,并将它们全部循环到您的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
属性设置为“Details”。

您必须遍历所有元素,并将所有元素循环到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
属性设置为“Details”。

您必须遍历所有元素,并将所有元素循环到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
属性设置为“Details”。

您必须遍历所有元素,并将所有元素循环到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
属性设置为“Details”。

我认为您需要这样的内容

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();
}
更新
或者把它移到这样的位置

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

我想你需要这样的东西

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();
}
更新
或者把它移到这样的位置

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

我想你需要这样的东西

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();
}
更新
或者把它移到这样的位置

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

我想你需要这样的东西

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();
}
更新
或者把它移到这样的位置

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

我在designer中制作。我在designer中制作。我在designer中制作。我在designer中制作。我喜欢这两种解决方案,它们都适合我,但Grundy的很容易阅读和理解。我更喜欢可读性,尤其是当它是方法形式时。干杯,谢谢。我喜欢这两种解决方案,都对我有用,但格伦迪的很容易阅读和理解。我更喜欢可读性,尤其是当它是方法形式时。干杯,谢谢。我喜欢这两种解决方案,都对我有用,但格伦迪的很容易阅读和理解。我更喜欢可读性,尤其是当它是方法形式时。干杯,谢谢。我喜欢这两种解决方案,都对我有用,但格伦迪的很容易阅读和理解。我更喜欢可读性,尤其是当它是方法形式时。干杯,谢谢。