title: XDocument Determine Last Child of Parent
body: <p>Walking an XML document with XDocument using descendants how would I determine I am at the last child of the parent?</p>
  
  <pre><code>XDocument xmlDoc = XDocument.Parse(xml)
  
    foreach (var element in xmlDoc.Descendants())
          {
             //Need to determine the last child of the parent and do something here. 
          }
  </code></pre>
  
  <p>Example of XML and what is expected to be returned.  I need to dynamically walk the document and not specify the XPath or Xquery since these will be used generically for various XML documents without getting into a lot of details of the why... Just looking to see if I can do this with XDocument or if there is a better approach still not specifying the tag names in a XPath or XQuery unless it can be done dynamically without knowing how many levels deep the document may be., </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Root&gt; 
  &lt;Customer&gt;
  &lt;CustID&gt;1&lt;/CustID&gt;
  &lt;Name&gt;Smith, Joe&lt;/Name&gt;
  &lt;CustomerTransDetail&gt;
  &lt;CustID&gt;1&lt;/CustID&gt;
  &lt;CustTransID&gt;1&lt;/CustTransID&gt;
  &lt;Note&gt;NA&lt;/Note&gt;
  &lt;/CustomerTransDetail&gt;
  &lt;CustomerTransDetail&gt;
  &lt;CustID&gt;1&lt;/CustID&gt;
  &lt;CustTransID&gt;2&lt;/CustTransID&gt;
  &lt;Note&gt;N/A&lt;/Note&gt;
  &lt;/CustomerTransDetail&gt;
  &lt;/Customer&gt;
  &lt;Customer&gt;
  &lt;CustID&gt;2&lt;/CustID&gt;
  &lt;Name&gt;Smith, Jane&lt;/Name&gt;
  &lt;CustomerTransDetail&gt;
  &lt;CustID&gt;2&lt;/CustID&gt;
  &lt;CustTransID&gt;1&lt;/CustTransID&gt;
  &lt;Note&gt;N/A&lt;/Note&gt;
  &lt;/CustomerTransDetail&gt;
  &lt;CustomerTransDetail&gt;
  &lt;CustID&gt;2&lt;/CustID&gt;
  &lt;CustTransID&gt;2&lt;/CustTransID&gt;
  &lt;Note&gt;N/A&lt;/Note&gt;
  &lt;/CustomerTransDetail&gt;
  &lt;/Customer&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>I need to know when I hit the last child of the parent so I can do something.  So every time I hit Name node or Note node for example I can do something. </p>