MxDanger
MxDanger

Reputation: 1

Xml File should create new line and leave the previous data

How can I write an XML file so that it starts a new line and does not overwrite the previous data?

string pfad = "C:\\temp\\Accounts.xml";

XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Login");
XmlElement id = doc.CreateElement("user");
XmlElement username = doc.CreateElement("username");
username.InnerText = txtBenutzerName.Text;

id.AppendChild(username);

root.AppendChild(id);
doc.AppendChild(root);
doc.Save(pfad);
MessageBox.Show("Created SuccesFully!");

Upvotes: 0

Views: 110

Answers (2)

Bikash
Bikash

Reputation: 39

  XmlDocument doc = new XmlDocument();
  doc.Load("C:\\Accounts.xml");
  doc.GetElementsByTagName("username")[0].InnerText="sdkjfsdknfkds";
  doc.Save("C:\\Accounts.xml");

Upvotes: 0

v.karbovnichy
v.karbovnichy

Reputation: 3306

Instead of creating a new document by this line

XmlDocument doc = new XmlDocument();

you should load existing file like this:

XmlDocument doc = new XmlDocument();
doc.Load(pfad);

...below code is the same...

Upvotes: 4

Related Questions