Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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# 如何使用Linq打开XML_C#_Xml_Linq - Fatal编程技术网

C# 如何使用Linq打开XML

C# 如何使用Linq打开XML,c#,xml,linq,C#,Xml,Linq,嗨,我想从xml中过滤atribute环境中的连接 但是我犯了一些错误,有人能帮我吗 这是我的密码: XElement-XElement=XElement.Load(xml); 变量连接=从xelement.Elements中的连接(“连接”) 其中(字符串)连接元素(“连接”)。属性(“环境”)=“测试” 选择conn; foreach(连接中的XElement连接) { MessageBox.Show(conconnect.Element(“conconnect”).Value); }您面

嗨,我想从xml中过滤atribute环境中的连接 但是我犯了一些错误,有人能帮我吗

这是我的密码:

XElement-XElement=XElement.Load(xml);
变量连接=从xelement.Elements中的连接(“连接”)
其中(字符串)连接元素(“连接”)。属性(“环境”)=“测试”
选择conn;
foreach(连接中的XElement连接)
{
MessageBox.Show(conconnect.Element(“conconnect”).Value);

}
您面临的问题(很可能)是“连接”是“连接”元素的集合,当您尝试按属性筛选它们时,您将只检查第一个元素。您需要首先使用SelectMany展平集合:

var connections = xelement.Elements("Conections")
            .SelectMany(c => c.Elements("Conection"))
            .Where(c => c.Attribute("Enviroment").Value == "Test");
然后在结果中,您将有一个类型为“Connection”的元素集合:


请使用
{}
,但不是问题中的html/javascript/css代码,而且您在问题标题中拼写错误了
how
,错误到底是什么?
foreach (var conection in connections)
{
    Console.WriteLine(conection); //a full element with Servidor, BaseDatos, Usuario and Password
    Console.WriteLine(conection.Element("Servidor").Value);    
}