title: Add an existing xml element to a xml document
body: <p>In this LINQ query I am creating a new XDocument for my own CD with track objects:</p>
  
  <pre><code>  var cdXml = new XDocument(new XElement("CD", new XAttribute("Artiest", c.Titel), new XAttribute("Naam", c.Naam),
              from t in c.tracks
              select new XElement("Tracks",
              new XElement("Track"),
              new XElement("Artiest", t.Artiest),
              new XElement("Titel", t.Titel),
              new XElement("Lengte", t.Lengte)
              )));
  </code></pre>
  
  <p>I am using an API from a music website and this is how I created the second XDocument from it:</p>
  
  <pre><code>    String xmlString;
      using (WebClient wc = new WebClient())
      {
          xmlString = wc.DownloadString(@"http://link-to-method");
      }
      XDocument myXMLDoc = XDocument.Parse(xmlString);
  </code></pre>
  
  <p>After that I want to add the tracks from the myXMLDoc to my own cdXml document, I already added 3 tracks to the cdXml document and I only want to add the tracks from the myXMLDoc who aren't already in my cdXml document.</p>
  
  <p>This is my query but it doesn't work:</p>
  
  <pre><code>            var query1 = from track in cdXml.Root.Elements("Tracks")
                          from track2 in myXMLDoc.Root.Element("album").Element("tracks").Elements("track")
                          where !track2.Element("name").Value.Contains(track.Element("Titel").Value)
                          select cdXml.Element("Tracks").Add(new XElement("Artiest", track2.Element("name").Value), 
  new XElement("Titel", track2.Element("artist").Element("name").Value), 
  new XElement("Lengte", track2.Element("duration").Value));
  </code></pre>
  
  <p>How do I add the existing elements from the myXMLDoc to my cdXml?</p>
  
  <p>This is the xml file from the API call from 2 tracks:</p>
  
  <pre><code>&lt;lfm status="ok"&gt;
      &lt;album&gt;
          &lt;name&gt;Awake&lt;/name&gt;
          &lt;artist&gt;Dream Theater&lt;/artist&gt;
          &lt;mbid&gt;e5544c68-43e9-4754-9239-b618454557f4&lt;/mbid&gt;
          &lt;url&gt;https://www.last.fm/music/Dream+Theater/Awake&lt;/url&gt;
          &lt;image size="small"&gt;https://lastfm-img2.akamaized.net/i/u/34s/96e5ac5821bf4a138aec1b8f80f25a6f.png&lt;/image&gt;
          &lt;listeners&gt;216679&lt;/listeners&gt;
          &lt;playcount&gt;6046178&lt;/playcount&gt;
          &lt;tracks&gt;
              &lt;track rank="1"&gt;
                  &lt;name&gt;6:00&lt;/name&gt;
                  &lt;url&gt;https://www.last.fm/music/Dream+Theater/_/6:00&lt;/url&gt;
                  &lt;duration&gt;331&lt;/duration&gt;
                  &lt;streamable fulltrack="0"&gt;0&lt;/streamable&gt;
                  &lt;artist&gt;
                      &lt;name&gt;Dream Theater&lt;/name&gt;
                      &lt;mbid&gt;28503ab7-8bf2-4666-a7bd-2644bfc7cb1d&lt;/mbid&gt;
                      &lt;url&gt;https://www.last.fm/music/Dream+Theater&lt;/url&gt;
                  &lt;/artist&gt;
              &lt;/track&gt;
              &lt;track rank="2"&gt;
                  &lt;name&gt;Caught in a Web&lt;/name&gt;
                  &lt;url&gt;https://www.last.fm/music/Dream+Theater/_/Caught+in+a+Web&lt;/url&gt;
                  &lt;duration&gt;328&lt;/duration&gt;
                  &lt;streamable fulltrack="0"&gt;0&lt;/streamable&gt;
                  &lt;artist&gt;
                      &lt;name&gt;Dream Theater&lt;/name&gt;
                      &lt;mbid&gt;28503ab7-8bf2-4666-a7bd-2644bfc7cb1d&lt;/mbid&gt;
                      &lt;url&gt;https://www.last.fm/music/Dream+Theater&lt;/url&gt;
                  &lt;/artist&gt;
              &lt;/track&gt;
      &lt;/album&gt;
  &lt;/lfm&gt;
  </code></pre>
  
  <p>I finally found my own solution:</p>
  
  <pre><code>var query = from track in myXMLDoc.Root.Element("album").Element("tracks").Elements("track")
                          join track2 in cdXml.Root.Elements("Tracks") on track.Element("name").Value equals track2.Element("Titel").Value into joinedT
                          from track2 in joinedT.DefaultIfEmpty()
                          where track2 == null
                          select track;
  </code></pre>