title: C# create nested XML from another XML
body: <p>I am trying to convert an XML to a nested XML with an element's attribute. I googled a lot and looked through some questions and answers here, but I still can't get my mind around it.</p>
  
  <p>I want to group the child node under same author's name using C#, linq to xml.</p>
  
  <p>Sample XML:</p>
  
  <pre><code>&lt;authors&gt;
    &lt;author name="John"&gt;
      &lt;books&gt;
         &lt;book type="Children"&gt;ABC&lt;/book&gt;
      &lt;/books&gt;
      &lt;published&gt; ---&lt;new
        &lt;print year="2011"&gt; ---&lt;new
           &lt;publisher&gt;Msoft&lt;/publisher&gt; ---&lt;new
        &lt;/print&gt; ---&lt;new
      &lt;/published&gt; ---&lt;new
    &lt;/author&gt;
    &lt;author name="May"&gt;
      &lt;books&gt;
         &lt;book type="Children"&gt;A beautiful day&lt;/book&gt;
      &lt;/books&gt;
      &lt;published&gt; ---&lt;new
        &lt;print year="2011"&gt; ---&lt;new
           &lt;publisher&gt;hardsoft&lt;/publisher&gt; ---&lt;new
        &lt;/print&gt; ---&lt;new
      &lt;/published&gt; ---&lt;new
    &lt;/author&gt;
    &lt;author name="John"&gt;
      &lt;books&gt;
         &lt;book type="Fiction"&gt;BBC&lt;/book&gt;
      &lt;/books&gt;
      &lt;published&gt; ---&lt;new
        &lt;print year="2013"&gt; ---&lt;new
           &lt;publisher&gt;dsney&lt;/publisher&gt; ---&lt;new
        &lt;/print&gt; ---&lt;new
      &lt;/published&gt; ---&lt;new
    &lt;/author&gt;
  &lt;/authors&gt;
  </code></pre>
  
  <p>Output expect:</p>
  
  <pre><code>&lt;authors&gt;
    &lt;author name="John"&gt;
      &lt;books&gt;
         &lt;book type="Children"&gt;ABC&lt;/book&gt;
         &lt;book type="Fiction"&gt;BBC&lt;/book&gt;
      &lt;/books&gt;
      &lt;published&gt;
        &lt;print year="2011"&gt;
           &lt;publisher&gt;Msoft&lt;/publisher&gt;
           &lt;publisher&gt;hardsoft&lt;/publisher&gt;
        &lt;/print&gt;
      &lt;/published&gt;
    &lt;/author&gt;
    &lt;author name="May"&gt;
      &lt;books&gt;
         &lt;book type="Children"&gt;A beautiful day&lt;/book&gt;
      &lt;/books&gt;
      &lt;published&gt;
        &lt;print year="2013"&gt;
           &lt;publisher&gt;dsney&lt;/publisher&gt;
        &lt;/print&gt;
      &lt;/published&gt;
    &lt;/author&gt;
  &lt;/authors&gt;
  </code></pre>
  
  <p>If there are additional nodes with attribute need to group under the same author, example  should I just add another grouping or select the element from the previous group?</p>
  
  <p>So far, I have tried:</p>
  
  <pre><code>XDocument doc = XDocument.Load(pathtoxmlfile);
  var query = from e in doc.Elements("author")
          group e by e.Attribute("name").Value into g
          select new XElement("author", new XAttribute("name", g.Key),
                 new XElement("books", 
                     g.Select(x =&gt; x.Element("books").Elements("book"))
                     , new XElement("published",
                           g.Select(y=&gt;y.Elements("publisher")
                     )
              )
          )
   );
  
   XElement root = new XElement("authors", query);
  </code></pre>
  
  <p>It only output me inside and author node with no entry.</p>
  
  <pre><code>&lt;author&gt;
    &lt;books&gt;...this part is output as expect...
    &lt;/books&gt;
    &lt;published&gt;
      &lt;publisher /&gt;
    &lt;/published&gt;
  &lt;/author&gt;
  </code></pre>