简体   繁体   中英

Conditional LINQ query on XML file

I would appreciate some help on a LINQ query I am doing on an XML file. I haven't been able to find anything in my searches that apply in my case but I have to admit I am still trying to figure out LINQ. So, i apologize in advance if this is is a repeat and I overlooked something.

I have an XML file that is created and filled at the time of the application starting. I am searching this XML file and trying to list all the "Connection" elements found under "Citrix" for a given hostname only.

So, for example, given the below XML file, I would want to only return
"desktop3" and "desktop4" if "client2" is selected.

Example of XML file:

<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>

I am able to get a list of all Citrix connections on all ThinClients, but I am struggling on getting just ones for a specified hostname.

The following returns all the Citrix connections for all hosts, but I am failing to understand how to take it further and only isolate it to hostname since the hostname is further up the XML chain.

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;
}

This should give you what you want :

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

Document.Elements(“ ThinClient”)。Where(e => e.Element(“ Hostname”)。Value == hostname)。剩余代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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