C# XML文件的条件LINQ查询

C# XML文件的条件LINQ查询,c#,xml,linq,C#,Xml,Linq,如果您能为我在XML文件上进行的LINQ查询提供帮助,我将不胜感激。在我的搜索中,我没有找到任何适用于我的情况的东西,但我必须承认,我仍在努力找出LINQ。所以,如果这是一个重复,我事先道歉,我忽略了一些东西 我有一个XML文件,它是在应用程序启动时创建和填充的。我正在搜索这个XML文件,并试图列出在“Citrix”下找到的仅针对给定主机名的所有“Connection”元素 例如,给定下面的XML文件,我只想返回 如果选择了“客户端2”,则为“桌面3”和“桌面4” XML文件的示例: <T

如果您能为我在XML文件上进行的LINQ查询提供帮助,我将不胜感激。在我的搜索中,我没有找到任何适用于我的情况的东西,但我必须承认,我仍在努力找出LINQ。所以,如果这是一个重复,我事先道歉,我忽略了一些东西

我有一个XML文件,它是在应用程序启动时创建和填充的。我正在搜索这个XML文件,并试图列出在“Citrix”下找到的仅针对给定主机名的所有“Connection”元素

例如,给定下面的XML文件,我只想返回
如果选择了“客户端2”,则为“桌面3”和“桌面4”

XML文件的示例:

<ThinClients>
  <ThinClient>
    <Status>OK</Status>
    <Hostname>client1</Hostname>
    <Citrix>
      <Connection>
        <ConnectionName>desktop1</ConnectionName>
        <XendesktopIP>192.168.0.10</XendesktopIP>
      </Connection>
      <Connection>
        <ConnectionName>desktop2</ConnectionName>
        <XendesktopIP>192.168.0.20</XendesktopIP>
      </Connection>
    </Citrix>
  <ThinClient>
  <ThinClient>
    <Status>OK</Status>
    <Hostname>client2</Hostname>
    <Citrix>
      <Connection>
        <ConnectionName>desktop3</ConnectionName>
        <XendesktopIP>192.168.0.30</XendesktopIP>
      </Connection>
      <Connection>
        <ConnectionName>desktop4</ConnectionName>
        <XendesktopIP>192.168.0.40</XendesktopIP>
      </Connection>
    </Citrix>
  <ThinClient>
<ThinClients>

好啊
客户1
桌面1
192.168.0.10
桌面2
192.168.0.20
好啊
客户2
桌面3
192.168.0.30
桌面4
192.168.0.40
我能够获得所有ThinClient上所有Citrix连接的列表,但我很难只获得指定主机名的连接

下面返回所有主机的所有Citrix连接,但我不知道如何进一步将其隔离到主机名,因为主机名在XML链的更上层

public ObservableCollection<CitrixConnections> GetCitrixConnectionListByHostname(string Hostname)
{
IEnumerable<CitrixConnections> clients =
    (from client in _Document.Elements("ThinClient").Elements("Citrix").Elements("Connection")
     select new CitrixConnections
     {
         ConnectionName = client.Element("ConnectionName").Value,
         XendesktopIP = client.Element("XendesktopIP").Value,
     });

var clientsAsObservableCollection = new ObservableCollection<CitrixConnections>(clients);
return clientsAsObservableCollection;
}
public observateCollection getCitrix ConnectionListByHostName(字符串主机名)
{
IEnumerable客户端=
(来自客户在文件元素(“ThincClient”).Elements(“Citrix”).Elements(“连接”)
选择新的Citrix连接
{
ConnectionName=client.Element(“ConnectionName”).Value,
XendesktopIP=client.Element(“XendesktopIP”).Value,
});
var clientsAsObservableCollection=新的ObservableCollection(客户);
返回客户端saobserveCollection;
}

Document.Elements(“ThinClient”).Where(e=>e.Element(“Hostname”).Value==Hostname)。剩余的代码应满足您的需求:

_Document
.Root
.Elements("ThinClient")
.Where(x => (string) x.Element("Hostname") == somevalue)
.SelectMany(x => x.Descendants("Connection"))
.Select(x => new CitrixConnections
             {
                ConnectionName = (string) x.Element("ConnectionName"),
                XendesktopIP = (string)x.Element("XendesktopIP")
             });

XDocument是你的朋友。你需要的确切语法我得去计算一下。谢谢你的快速回复。这确实起了作用,并节省了我更多的头发被拔出来!我只需要在上面添加一个,然后再添加一个。在where和SelectMany行之间选择many行。具体来说,.SelectMany(x=>x.substands(“Citrix”))。再次感谢!!