title: linq query to parse given complex xml
body: <p>I have a xml like below with root as rail</p>
  
  <pre><code>        &lt;rail&gt;
          &lt;timetable&gt;
           &lt;trainParts&gt;
                &lt;trainPart id="tp_1" name="1" timetablePeriodRef="ttp_2012_13" categoryRef="cat_Commuter" processStatus="planned" trainNumber="1"&gt;
                  &lt;operatingPeriodRef ref="Daily" /&gt;
                  &lt;ocpsTT&gt;
                    &lt;ocpTT ocpType="begin" ocpRef="ocp_SWH"&gt;
                      &lt;sectionTT trackInfo="SWH-DM" /&gt;
                    &lt;/ocpTT&gt;
                    &lt;ocpTT ocpType="stop" ocpRef="ocp_SE"&gt;
                       &lt;times arrival="16:16:00" departure="16:18:00" scope="scheduled" /&gt;
                      &lt;sectionTT trackInfo="SE-DM" /&gt;
                    &lt;/ocpTT&gt;
      .
      .
      .
      so on
  &lt;/ocpsTT&gt;
           &lt;/trainPart&gt;
          &lt;/trainParts&gt;
          &lt;/timetable&gt;
          &lt;/rail&gt;
  </code></pre>
  
  <p><strong>Now like this there are many train numbers whose details I have to parse in one go.</strong>
  I can parse one child and its attributes at a time using linq but i want to parse all the childs and its elements.
  Say for trainNumer="1" i need to get </p>
  
  <pre><code>categoryRef
      processStatus
  
      operatingPeriodRef
  
      ocpType
      ocpRef
      trackInfo
  
      arrival
      departure
  </code></pre>
  
  <p><strong>NOTE: In some cases times tag containing departure arrival is not there</strong></p>
  
  <p>I did try to write the code as below:</p>
  
  <pre><code>   public void trainDetails(string trainNumber)
              {
  
                  var xdoc = XDocument.Load("Rail.xml");
                  XNamespace ad = "http://www.rail.org/schemas/2009";
                   var train = (from t in xdoc.Root.Elements(ad + "timetable")
                           let d = t.Element(ad + "trainParts").Element("trainPart")
                           where (string)t.Attribute("number") == trainNumber 
                           select new
                           {
                               operatingPeriod=(from s1 in d.Elements(ad+"operatingPeriodRef")
                                                operatingref=(string)s1.Attribute("ref")
                                                 }).ToList()
                               }
                           select new
                           {
                              trainOcpsTT= (from s2 in d.Elements(ad + "ocpsTT").Elements(ad+"ocpTT")
                                               select new
                                               {
                                                  ocpType=(string)s2.Attribute("ocpType"),
                                                  ocpRef=(string)s2.Attribute("ocpRef")
                                                }).ToList()
                               }).FirstOrDefault();
  
          }
             }
  </code></pre>
  
  <p>I am unable to frame the query properly..
  Is it Possible to get all these in one xml linq query itself?How?
  If not then which is the proper approach in this case..</p>