title: Inserting new XML Data at specific Point in document C# Using LINQ
body: <p>Hello everyone I have been searching all day looking for a way to add data into a existing XML document using LINQ. All that I can seem to find is how to create new elements and how to add them to just the end of the file if you will.  I am trying to create an application that will allow me to add FTP accounts and update existing ones from Filezilla Ftp client without manually having to do so. Here is the XML document.                 </p>
  
  <pre><code>    &lt;FileZillaServer&gt;
    &lt;Settings&gt;
      &lt;Item name="Admin port" type="numeric"&gt;14147&lt;/Item&gt;
    &lt;/Settings&gt;
    &lt;Groups /&gt;
    &lt;Users&gt;
      &lt;User Name="ServerManager"&gt;
        &lt;Option Name="Pass"&gt;6&lt;/Option&gt;
        &lt;Option Name="Group"&gt;
        &lt;/Option&gt;
        &lt;Option Name="Bypass server userlimit"&gt;0&lt;/Option&gt;
        &lt;Option Name="User Limit"&gt;0&lt;/Option&gt;
        &lt;Option Name="IP Limit"&gt;0&lt;/Option&gt;
        &lt;Option Name="Enabled"&gt;1&lt;/Option&gt;
        &lt;Option Name="Comments"&gt;
        &lt;/Option&gt;
        &lt;Option Name="ForceSsl"&gt;0&lt;/Option&gt;
        &lt;IpFilter&gt;
          &lt;Disallowed /&gt;
          &lt;Allowed /&gt;
        &lt;/IpFilter&gt;
        &lt;Permissions&gt;
          &lt;Permission Dir="C:\Dayz Server Manager"&gt;
            &lt;Option Name="FileRead"&gt;1&lt;/Option&gt;
            &lt;Option Name="FileWrite"&gt;1&lt;/Option&gt;
            &lt;Option Name="FileDelete"&gt;1&lt;/Option&gt;
            &lt;Option Name="FileAppend"&gt;1&lt;/Option&gt;
            &lt;Option Name="DirCreate"&gt;1&lt;/Option&gt;
            &lt;Option Name="DirDelete"&gt;1&lt;/Option&gt;
            &lt;Option Name="DirList"&gt;1&lt;/Option&gt;
            &lt;Option Name="DirSubdirs"&gt;1&lt;/Option&gt;
            &lt;Option Name="IsHome"&gt;1&lt;/Option&gt;
            &lt;Option Name="AutoCreate"&gt;0&lt;/Option&gt;
          &lt;/Permission&gt;
          &lt;Permission /&gt;
        &lt;/Permissions&gt;
        &lt;SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0"&gt;
          &lt;Download /&gt;
          &lt;Upload /&gt;
        &lt;/SpeedLimits&gt;
      &lt;/User&gt;
    &lt;/Users&gt;
  &lt;/FileZillaServer&gt;
  </code></pre>
  
  <p>I have so far been able to figure out how to get into the correct place of the document using a LINQ query but not really sure how to add to it.  Any help would be greatly appreciated.  Below is the Query I'm using to get me in the right place.</p>
  
  <pre><code>                var InsHere = from name in doc.Descendants("Users")
                             where name.Element("User").Attribute("Name").Value == "ServerManager"
                             select name.Element("User").Element("Permissions")
                             .Element("Permission").Attribute("Dir").Value;
  </code></pre>
  
  <p>So in this case what I'm needing to do is would be add another directory to the  for a specific user that will change on a consistent basis.  Many situations will require that I create a whole new user.  Thank you in advance for any help.</p>
  
  <p>Okay so after looking at the examples and the answers given I think I have begin to get closer.  However I am still getting either a "Parent is Missing" Exception or "Object reference" Error.  Here is the code below that I am using to try and insert.</p>
  
  <pre><code>            try
          {
  
              XDocument doc = XDocument.Load("C:/users/vildez/desktop/test.xml");
  
              XElement test = new XElement("TEST", "this is data");
  
              var InsertPoint = from user in doc.Descendants("Users")
                                where user.Element("User").Attribute("Name").Value == "ServerManager"
                                select user.Element("User").Element("Permissions").Element("Permission").Name;
  
              foreach (var v in InsertPoint)
              {
                  XElement Perm = doc.Element(v.ToString());
                  Perm.AddAfterSelf(test);
              }
  
              doc.Save("C:/users/vildez/desktop/123.xml");
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
              Application.Exit();
          }
  </code></pre>