title: Grouping of Xml Elements using Linq
body: <p>I am having following xml.What i need to do is,i want to <code>group</code> rows in each table using  their <code>locid</code> using linq.</p>
  
  <pre><code>&lt;taxdata schema="3" nextrowid="812" timesaved="0" calcmode="1" calcneeded="0"&gt;
    &lt;table name="aatbl_entity" id="1" ver="102" ddtime="11-10-2012 17:57:08" locs="" /&gt; 
    &lt;table name="aatbl_entityactivity" id="2" ver="102" ddtime="11-10-2012 17:57:08" locs="" key0="2"&gt;
          &lt;row name="" locid="100" rows="71" rowid="71" Ks0="1"&gt; &lt;/row&gt;
          &lt;row name="" locid="48" rows="52" rowid="52" Ks0="8"&gt; &lt;/row&gt;
          &lt;row name="" locid="6148" rows="216" rowid="216" Ks0="16"&gt; &lt;/row&gt;
          &lt;row name="" locid="6148" rows="217" rowid="217" Ks0="17"&gt; &lt;/row&gt;
          &lt;row name="" locid="6148" rows="218" rowid="218" Ks0="18"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6154" rows="219" rowid="219" Ks0="19"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6154" rows="220" rowid="220" Ks0="20"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6154" rows="221" rowid="221" Ks0="21"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6154" rows="222" rowid="222" Ks0="22"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6154" rows="223" rowid="223" Ks0="23"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6209" rows="224" rowid="224" Ks0="24"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6127" rows="233" rowid="233" Ks0="32"&gt;   &lt;/row&gt;
          &lt;row name="" locid="6127" rows="234" rowid="234" Ks0="33"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6127" rows="235" rowid="235" Ks0="34"&gt;  &lt;/row&gt;
          &lt;row name="" locid="36" rows="236" rowid="236" Ks0="35"&gt;  &lt;/row&gt;
          &lt;row name="" locid="36" rows="244" rowid="244" Ks0="43"&gt;  &lt;/row&gt;
          &lt;row name="" locid="111" rows="266" rowid="266" Ks0="51"&gt;  &lt;/row&gt;
          &lt;row name="" locid="111" rows="752" rowid="752" Ks0="59"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6192" rows="361" rowid="361" Ks0="55"&gt;  &lt;/row&gt;
          &lt;row name="" locid="10" rows="810" rowid="810" Ks0="60"&gt; &lt;/row&gt;
          &lt;row name="" locid="10" rows="811" rowid="811" Ks0="61"&gt; &lt;/row&gt;
          &lt;row name="" locid="10" rows="812" rowid="812" Ks0="62"&gt;  &lt;/row&gt;
    &lt;/table&gt; 
   &lt;table name="aatbl_entityactivitydetail" id="13" ver="102" ddtime="11-10-2012 17:57:09" locs="" key0="2" key1="5"&gt;
          &lt;row name="" locid="6327" rows="90" rowid="90" Ks0="1" Ks1="1"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6327" rows="104" rowid="104" Ks0="1" Ks1="2"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6327" rows="118" rowid="118" Ks0="1" Ks1="3"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6327" rows="133" rowid="133" Ks0="1" Ks1="4"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6327" rows="522" rowid="522" Ks0="3" Ks1="1"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6327" rows="529" rowid="529" Ks0="3" Ks1="2"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6327" rows="536" rowid="536" Ks0="3" Ks1="3"&gt;  &lt;/row&gt;
          &lt;row name="" locid="6327" rows="543" rowid="543" Ks0="3" Ks1="4"&gt;  &lt;/row&gt;
          &lt;row name="" locid="101" rows="177" rowid="177" Ks0="1" Ks1="5"&gt;  &lt;/row&gt;
          &lt;row name="" locid="101" rows="563" rowid="563" Ks0="3" Ks1="5"&gt;  &lt;/row&gt;
          &lt;row name="" locid="105" rows="185" rowid="185" Ks0="1" Ks1="6"&gt;  &lt;/row&gt;
          &lt;row name="" locid="105" rows="564" rowid="564" Ks0="3" Ks1="6"&gt;  &lt;/row&gt;
          &lt;row name="" locid="106" rows="186" rowid="186" Ks0="1" Ks1="7"&gt;  &lt;/row&gt;
          &lt;row name="" locid="106" rows="565" rowid="565" Ks0="3" Ks1="7"&gt;  &lt;/row&gt;
          &lt;row name="" locid="106" rows="2" rowid="2" Ks0="3" Ks1="7"&gt; &lt;/row&gt;
    &lt;/table&gt;
    &lt;table name="aatbl_entityactivityasset" id="14" ver="102" ddtime="11-10-2012 17:57:09" locs="" key0="2" key1="9" 
  &lt;/taxdata&gt;
  </code></pre>
  
  <p>I tried following bur it didn't work,</p>
  
  <pre><code>const string filePath = @"D:\XJH\11-10-2012_17_57_08.xml";
  
  XDocument doc = XDocument.Load(filePath);
  
  var tableIds =
      doc
          .Descendants("table")
          .Select(table =&gt; new { id = table.Attributes("id").ToString() });
  
  foreach (var id in tableIds)
  {
     var rowgroups =doc.Descendants("table")
                       .Elements()
                       .Where(row =&gt; row.Ancestors()
                              .Attributes("id").ToString() == id.ToString())
                       .GroupBy(row =&gt; new { ids = row.Attributes("locid") })
                       .Select(row =&gt; row);
  }
  </code></pre>
  
  <p>I don't know whether this approach for grouping is correct or not,please help....</p>