title: Updating current XML file through C#
body: <p>I am having trouble manipulating my code to update an existing xml file with more data. My code is as below and I used an if statement, but I get an error which causes the programme to crash. Can somebody please help me as to where I am going wrong...</p>
  
  <p>Code samples</p>
  
  <pre><code>XDocument xmlDoc = XDocument.Load("order.xml"); //Loads the xml file.
  
          XElement xml = new XElement("MenuInfo",
              new XElement("Meal",
              new XElement("Data1", mealListView.SelectedValue.ToString()),
              new XElement("Data2", _seat),
              new XElement("Data3", buttonTable1.Text)
              )
              );
          if (xmlDoc == null)
          {
              xml.Save("order.xml");
          }
          else
          {
              xmlDoc.Add(xml);
          }
  </code></pre>