I am creating a sample login form in a windows forms application & I am trying to save my login username or password in a XML file but when I submit username or password it replaces the previous entry.
What I want is that when I submit username or password it should be appended into the same XML file but don’t replace any previous entry.
Like:
<Table>
<Entry>
<User>first_user1@gmail.com</User>
<Password>*********</Password>
</Entry>
</Table>
<Table>
<Entry>
<User>second_user2@gmail.com</User>
<Password>*********</Password>
</Entry>
</Table>
But its replacing the first enty every time and not giving the space for second entry
Please give me solution for this
This is my code:
private void button1_Click(object sender, EventArgs e)
{
XmlTextWriter writer = new XmlTextWriter("entry.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("Table");
createNode(textBox1.Text, textBox2.Text, writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
MessageBox.Show("XML File created ! ");
}
private void createNode(string pName, string pPass, XmlTextWriter writer)
{
writer.WriteStartElement("Entry");
writer.WriteStartElement("User");
writer.WriteString(pName);
writer.WriteEndElement();
writer.WriteStartElement("Password");
writer.WriteString(pPass);
writer.WriteEndElement();
writer.WriteEndElement();
}
Use LINQ To XML. It’s much more flexible:
and then
So, if your file was:
you get an output: