简体   繁体   中英

How to select child nodes when parent node equals a specific value in LINQ to XML

Part of an application I am building needs to generate a robots.txt file from an XML document.

I have XML like so :

<root>
  <Robots>
    <UserAgents>
      <UserAgent>*</UserAgent>
      <Disallow>
        <Item>/wibble/</Item>
        <Item>/wobble/</Item>
      </Disallow>
    </UserAgents>
    <UserAgents>
      <UserAgent>Google</UserAgent>
      <Disallow>
        <Item>/</Item>
      </Disallow>
    </UserAgents>
  </Robots>
</root>

How can I select the "Items" where the UserAgent = "*"?

In other words I want to select the child nodes only when a parent node equals a certain value. The value in the parent node will be unique.

I am using C# in ASP.net.

Sounds like you want something like:

var query = from agent in doc.Descendants("UserAgent")
            where (string) agent == "*"
            from item in agent.Parent.Elements("Disallow").Elements("Item") 
            select item;

Note that the second from is just used to flatten the result so it isn't a sequence of sequences.

Alternatively:

var query = doc.Descendants("UserAgent")
               .Where(agent => (string) agent == "*")
               .SelectMany(agent => agent.Parent.Elements("Disallow")
                                                .Elements("Item"));

Or by changing the "top" node selected to UserAgents , you can avoid the parent step:

var query = doc.Descendants("UserAgents")
               .Where(agents => (string) agents.Element("UserAgent") == "*")
               .SelectMany(agent => agent.Elements("Disallow").Elements("Item"));

Note that that won't give the same results if you have multiple UserAgent elements under a single UserAgents element.

(Are you able to change the XML schema, btw? It looks like you should be able to simplify this...)

var query = from x in doc.Root.Element("Robots").Elements("UserAgents")
            where (string)x.Element("UserAgent") == "*"
            from y in x.Element("Disallow").Elements("Item")
            select y;

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