简体   繁体   English

C#如何在另一个Linq语句中的XmlAttributeCollection上使用Linq?

[英]C# How to use Linq on an XmlAttributeCollection within another Linq statement?

I have an XML element with subelements like so: 我有一个带有子元素的XML元素,如下所示:

<Groups>
    <Group1 name="first" value="1" />
    <Group2 value="2" name="second" />
    <Group3 value="3" />
</Groups>

I'm using an already existing method MyMethod() to fetch the Groups element which returns an XmlNodeList object which I cast to an XmlNode . 我使用一个已经存在的方法MyMethod()来获取Groups元素,该元素返回一个XmlNodeList对象,该对象已转换为XmlNode After that I want to use a Linq statement to fetch only those groups which have a name attribute in them and store those names in a list of strings. 之后,我想使用Linq语句仅获取其中具有name属性的那些组,并将这些名称存储在字符串列表中。

In the code snippet below I'm trying to check if the first attribute's name of an XML node equals "name" but it is possible that the "name" attribute isn't always the first one. 在下面的代码片段中,我试图检查XML节点的第一个属性的名称是否等于"name"但是"name"属性可能并不总是第一个。 Could you please help me out here and tell me how to use another Linq statement on Attributes property below? 您能否在这里帮助我,并告诉我如何在下面的Attributes属性上使用另一个Linq语句? Attributes property is of type XmlAttributeCollection . Attributes属性的类型为XmlAttributeCollection

List<string> result = MyMethod().Cast<XmlNode>()
    .Where(node => node.Attributes[0].Name == "name")
    .Select(node => node.Attributes[0].Value).ToList();

EDIT: I managed to find a solution using the built in method GetNamedItem : 编辑:我设法找到一个使用内置方法GetNamedItem的解决方案:

List<string> result = MyMethod().Cast<XmlNode>()
    .Where(node => node.Attributes?.GetNamedItem("name") != null)
    .Select(node => node.Attributes?.GetNamedItem("name").Value).ToList();

What do you think about that: 您对此有何看法:

List<string> result = MyMethod().Cast<XmlNode>()
    .SelectMany(node => node.Attributes).Where(a => a.Name == "name");

Should match your goal to got the object with the specific name, the full code become: 应该匹配您的目标以获取具有特定名称的对象,完整代码变为:

List<string> result = MyMethod().Cast<XmlNode>()
        .SelectMany(node => node.Attributes).Where(a => a.Name == "name")
        .Select(a=> a.Value).ToList();

Key method for your question is extension method .SelectMany which return flattened collection from collection of collections. 您问题的关键方法是扩展方法.SelectMany ,它从集合的集合中返回展平的集合。

As alternative you can use LINQ to XML (sounds like right tool for the job ;)) 作为替代方案,您可以使用LINQ to XML(听起来像是完成这项工作的正确工具;)

var document = XDocument.Load("pathToXmlFile");

// Here you can use more complex logic of "MyMehthod" 
var allGroups = document.Descendants("Groups");

var names = allGroups.SelectMany(groups => groups.Elements())
                     .Where(group => group.Attribute("name"))
                     .Where(attribute => attribute != null)
                     .Select(attribute => attribute.Value)
                     .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM