How to add new information in XML using serialization without overwriting previous information

Asked

Viewed 25 times

0

Good evening guys, I am casually studying and creating some applications, I have reached a point where I need to create an XML with values, and that every time I enter information it goes to the XML file without erasing the previous information.

I’m using something very simple in the code:

public string file = "XML.xml";
public class Pessoas
{
    public string Nome { get; set; }
    public int Idade { get; set; }
}
public Form1()
{
    InitializeComponent();
    Pessoas p = new Pessoas() { Nome = "Fulano", Idade = 50 };
    XmlSerializer xml = new XmlSerializer(typeof(Pessoas));
    StreamWriter writer = new StreamWriter(file);
    xml.Serialize(writer, p);
    writer.Close();
}

it generates a standard XML file, but every time I change the value of the name and age in the code it modifies what has already been entered file.

  • The idea there would be to use a Reader, to stick what is in the file inside an object/collection in memory.. make your modifications and then write the changed object in the file, subscribing, but including what already existed. But recommend using JSON, which is a much more widely used format than XML, in general.

  • I get it, I’m seeing json as you suggested, I found it very quiet to work, you have some support material to indicate me ?

  • You can’t use any kind of database for that? Persisting your application information in a text file is a bad idea and a beautiful invitation to have competition problems and even data loss.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.