title: How do I remove a child element based on attributes on multiple parents?
body: <p>I have an XML file that will be similar to the structure below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Root Attr1="Foo" Name="MyName" Attr2="Bar" &gt;
      &lt;Parent1 Name="IS"&gt;
          &lt;Child1 Name="Kronos1"&gt;
              &lt;GrandChild1 Name="Word_1"/&gt;
              &lt;GrandChild2 Name="Word_2"/&gt;
              &lt;GrandChild3 Name="Word_3"/&gt;
              &lt;GrandChild4 Name="Word_4"/&gt;
          &lt;/Child1&gt;
          &lt;Child2 Name="Kronos2"&gt;
              &lt;GrandChild1 Name="Word_1"/&gt;
              &lt;GrandChild2 Name="Word_2"/&gt;
              &lt;GrandChild3 Name="Word_3"/&gt;
              &lt;GrandChild4 Name="Word_4"/&gt;
          &lt;/Child2&gt;
      &lt;/Parent1&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>The elements are not defined in that they can have different values than other files.  What I do know is the "Name" attribute of each element before hand, which will always be defined.  I need to be able to manipulate, and/or delete data within a selected element based on that name. For Example:  <code>removeElement("MyName.IS.Kronos1.Word_1")</code> would delete the <code>GrandChild1</code> element underneath the <code>Child1</code> Parent.  </p>
  
  <p>My issues is that while using LINQ to XML queries I'm not able to select that element properly.  Using this:</p>
  
  <pre><code>private IEnumerable&lt;XElement&gt; findElements(IEnumerable&lt;XElement&gt; docElements, string[] names)
  {
      // the string[] is an array from the desired element to be removed.
      // i.e. My.Name.IS ==&gt; array[ "My, "Name", "IS"]
      IEnumerable&lt;XElement&gt; currentSelection = docElements.Descendants();
  
      foreach (string name in names)
      {
          currentSelection =
              from el in currentSelection
              where el.Attribute("Name").Value == name
              select el;
      }
  
      return currentSelection;
  
  }
  </code></pre>
  
  <p>To find where I need to remove the elements yields this result:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Root Attr1="Foo" Name="MyName" Attr2="Bar" &gt;
      &lt;Parent1 Name="IS"&gt;
          &lt;Child1 Name="Kronos1"&gt;
              &lt;GrandChild2 Name="Word_2"/&gt;
              &lt;GrandChild3 Name="Word_3"/&gt;
              &lt;GrandChild4 Name="Word_4"/&gt;
          &lt;/Child1&gt;
          &lt;Child2 Name="Kronos2"&gt;
              &lt;GrandChild2 Name="Word_2"/&gt;
              &lt;GrandChild3 Name="Word_3"/&gt;
              &lt;GrandChild4 Name="Word_4"/&gt;
          &lt;/Child2&gt;
      &lt;/Parent1&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>After debugging it appears that all I'm doing is searching for the same document over again, but for different names each time.  How do I search and select a specific element based on multiple parent attribute Names?</p>
  
  <p>It should be noted, that the size of the XML (meaning levels of elements) are also variable.  Meaning that there can as little as 2 levels (Parents) or up to 6 levels (Great-Great-GrandChildren).  However, I NEED to be able to look at the root node's <code>Name</code> Attribute as well.</p>