title: Writing to XML using XDocument, but knowing where to write
body: <p>Hope you can help me a bit. I'm trying to write to an XML file, but am struggling to write the method which, well, writes to the XML file. This is the XML file manually written (using Notepad++ etc.):</p>
  
  <pre><code>&lt;software&gt;
      &lt;software_entry
      name="Adobe Acrobat X Standard"
      path="Applications\Acrobat\Acrobat X Standard\AcroStan.msi" 
      type="msi"
      switches="/qn ALLUSERS=1"
      /&gt;
  
      &lt;software_entry
      name="Adobe Acrobat X Professional"
      path="Applications\Acrobat\Acrobat X Pro\AcroPro.msi"
      type="msi"
      switches="/qn ALLUSERS=1"
      /&gt;
  &lt;/software&gt;
  </code></pre>
  
  <p>The aim of this part of the application is to write that using a GUI.</p>
  
  <p>In the application, the user chooses the name of the XML file. It is then saved in the temp folder until further in the process when the user is asked where they would like to save it. Upon entering the desired name of the file and clicking Create, the method called "createAndLoadXML" is run. As its name would suggest, it creates and then loads an XML file (to populate a listview control on the form). Code can be seen below.</p>
  
  <pre><code>private void createAndLoadXML()
  {
      // Method to create XML file based on name entered by user
      string tempPath = Path.GetTempPath();
      string configFileName = fileNameTextBox.Text;
      string configPath = tempPath + configFileName + ".xml";
      // Create XDocument
      XDocument document = new XDocument(
          new XDeclaration("1.0", "utf8", "yes"),
          new XComment("This XML file defines the software selections for use with the Software Installer"),
          new XComment("XML file generated by Software Installer"),
          new XElement("software",
              new XElement("software_entry",
                  new XAttribute("name", ""),
                  new XAttribute("path", ""),
                  new XAttribute("type", ""),
                  new XAttribute("switches", ""))
                  )
          );
      document.Save(configPath);
      configCreateLabel.Visible = true;
      document = XDocument.Load(configPath);
  }
  </code></pre>
  
  <p>Now, further down this form are 4 text boxes for user input, each relating to the attributes created (name, path, type and switches) The idea is the user will write in these text boxes, click an 'Add' button and then the program will write those 4 fields as attributes to this XML file. So far, I have this code, which is horribly incomplete and doesn't even use LINQ to XML.</p>
  
  <pre><code>private void writeToXML()
  {
      // Method to write lines to XML file based on user input
      // Sets string variables
      string fileName = softwareNameTextBox.Text;
      string filePath = filePathTextBox.Text;
      string fileType = installerType.Text.ToString();
      string installSwitches = installSwitchesTextBox.Text;
      using (XmlWriter xw = XmlWriter.Load(configPath)) //This line is wrong, I know
      {
          xw.WriteStartElement("software");
          xw.WriteElementString("name", fileName);
          xw.WriteElementString("path", filePath);
          xw.WriteElementString("type", fileType);
          xw.WriteElementString("switches", installSwitches);
          xw.WriteEndElement();
      }
  }
  </code></pre>
  
  <p>Basically, could anyone please help me with the above method which writes to the XML the data the user has entered into the text box controls? I'm not sure how to load the previously created XML document (from my createAndLoadXML method), and how to write within the root element (software) using LINQ to XML.</p>