Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择作为具有属性的最后一个子元素的元素_C#_Linq_Linq To Xml - Fatal编程技术网

C# 选择作为具有属性的最后一个子元素的元素

C# 选择作为具有属性的最后一个子元素的元素,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,任何帮助都将不胜感激;另外,我对LINQ to XML完全不熟悉,我找不到一个网页可以从中获得“Any”的确切含义,如果有其他操作符,那么它们是什么。您的XML片段需要一个顶级元素,这可能很重要。如果您将上面的内容包装在一个外部标记中,那么如果您从任何没有模式属性的端口元素捕获空引用,那么您的代码似乎可以工作。例如 var modbusportSelected = Elements("bus").Elements("port") .Where( x => x.Elements("req")

任何帮助都将不胜感激;另外,我对LINQ to XML完全不熟悉,我找不到一个网页可以从中获得“Any”的确切含义,如果有其他操作符,那么它们是什么。

您的XML片段需要一个顶级元素,这可能很重要。如果您将上面的内容包装在一个外部标记中,那么如果您从任何没有
模式
属性的
端口
元素捕获空引用,那么您的代码似乎可以工作。例如

var modbusportSelected = Elements("bus").Elements("port")
.Where( x => x.Elements("req")
.Any(y => y.Attribute("mode").Value.Contains("read")))
.Last();

您的XML片段需要一个顶级元素,这一点可能很重要。如果您将上面的内容包装在一个外部标记中,那么如果您从任何没有
模式
属性的
端口
元素捕获空引用,那么您的代码似乎可以工作。例如

var modbusportSelected = Elements("bus").Elements("port")
.Where( x => x.Elements("req")
.Any(y => y.Attribute("mode").Value.Contains("read")))
.Last();

Any只返回bool,条件是在Any方法中设置的。在您的情况下,您会说是否有任何模式属性的值包含read.Thank!您还知道我在哪里可以找到关于任何运算符和其他运算符的适当文档吗?Any只返回bool,条件是从Any方法中设置的。在您的情况下,您会说是否有任何模式属性的值包含read.Thank!你还知道我在哪里可以找到一些关于任何运营商和其他运营商的像样的文档吗?
using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApp1
{
    class Program
    {
        public static string xml = @"<topLevel><bus>
    <port isCorrectNode='no'>
        <req>
            <item>
            </item> 
        </req>
        <req mode='read'>
            <item>
            </item> 
        </req>
    </port>
    <port isCorrectNode='yes'>
        <req mode='read'>
            <item>
            </item> 
        </req>
        <req>
            <item>
            </item> 
        </req>
    </port>
</bus>
<bus>
</bus>
</topLevel>";

        static void Main(string[] args)
        {
            XElement root = XElement.Parse(xml);

            var found = root.Elements("bus").Elements("port")
                .Where(x => x.Elements("req").Any(y => y.Attribute("mode") != null && y.Attribute("mode").Value.Contains("read")))
                .Last();

            var isThisTheCorrectNode = found.Attribute("isCorrectNode").Value;
            Console.WriteLine(isThisTheCorrectNode);
        }
    }
}
var wanted = root.Elements("bus").Elements("port")
    .Where(x => x.Elements("req").Any() && // make sure there is a req element
x.Elements("req").Last().Attribute("mode") != null && // and it has the attribute  
x.Elements("req").Last().Attribute("mode").Value.Contains("read")) // and it has the right value
    .Last();