title: Writing textbox values to xml file in C#.net
body: <p>I have two TextBoxes namely <code>txtUserid</code> and <code>txtPassowrd</code>.</p>
  
  <p>I'm writing the values entered in textboxes to  a <code>XML</code> file but I do not want the same <code>txtuserid</code> values to be written twice in <code>XML</code> - it should be overwritten. </p>
  
  <p><strong>For example :</strong></p>
  
  <ul>
  <li>If I enter in <code>txtUserid=2</code> and <code>txtPassword=I</code></li>
  <li>and the second time if I enter <code>txtUserid=2</code> and  <code>txtPassword=m</code></li>
  </ul>
  
  <p>then I only want one entry to be kept in the <code>XML</code> file : </p>
  
  <p><strong>For the above example:</strong> the <code>txtUserid=2</code> and <code>textPassword=m</code></p>
  
  <p>The code:</p>
  
  <pre><code>XDocument Xdoc = new XDocument(new XElement("Users"));
  if (System.IO.File.Exists("D:\\Users.xml"))
  {
     Xdoc = XDocument.Load("D:\\Users.xml");
  }
  else
  {
     Xdoc = new XDocument();
  }
  
  XElement xml = new XElement("Users",
                 new XElement("User",
                 new XAttribute("UserId", txtUserName.Text),
                 new XAttribute("Password", txtPassword.Text)));
  
  if (Xdoc.Descendants().Count() &gt; 0)
  {
     Xdoc.Descendants().First().Add(xml);
  }
  else
  {
     Xdoc.Add(xml);
  }
  
  Xdoc.Save("D:\\Users.xml");
  </code></pre>