Q:

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. 

A:

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

