Ver Mensaje Individual
  #1  
Antiguo 11-02-2023
REHome REHome is offline
Miembro
 
Registrado: jul 2003
Ubicación: España
Posts: 454
Reputación: 21
REHome Va por buen camino
¿Cuál es el mejor archivo para guardar y recuperar datos?

Hola:

Antes usaba muchos el XML para guardar y leer datos que tenía en el formulario. Por ejemplo en este caso.



Código:
using System;
using System.Windows.Forms;
using System.Xml;

namespace Guardar_y_cargar_datos_en_archivo_xml
{
 * *public partial class Form1 : Form
 * *{
 * * * *public Form1()
 * * * *{
 * * * * * *InitializeComponent();
 * * * *}

 * * * *// Guarda los datos introducidos a parte de crear el archivo "miXML.xml".
 * * * *private void button_Guardar_Click(object sender, EventArgs e)
 * * * *{
 * * * * * *XmlWriter w = XmlWriter.Create("miXML.xml");
 * * * * * *w.WriteStartElement("MiInfo");
 * * * * * *w.WriteElementString("TextBox", textBox1.Text);
 * * * * * *w.WriteElementString("CheckBox", checkBox1.Checked.ToString());
 * * * * * *w.WriteElementString("radioButton1", radioButton1.Checked.ToString());
 * * * * * *w.WriteElementString("radioButton2", radioButton2.Checked.ToString());
 * * * * * *w.WriteEndElement();
 * * * * * *w.Close();
 * * * *}

 * * * *private void Form1_Load(object sender, EventArgs e)
 * * * *{
 * * * * * *try
 * * * * * *{
 * * * * * * * *XmlReader r = XmlReader.Create("miXML.xml");
 * * * * * * * *r.ReadStartElement("MiInfo");
 * * * * * * * *textBox1.Text = r.ReadElementContentAsString();
 * * * * * * * *checkBox1.Checked = Convert.ToBoolean(r.ReadElementContentAsString());
 * * * * * * * *radioButton1.Checked = Convert.ToBoolean(r.ReadElementContentAsString());
 * * * * * * * *radioButton2.Checked = Convert.ToBoolean(r.ReadElementContentAsString());
 * * * * * * * *r.ReadEndElement();
 * * * * * * * *r.Close();
 * * * * * *}
 * * * * * *catch (Exception)
 * * * * * *{

 * * * * * * * *//
 * * * * * *}

 * * * *}
 * *}
}
El archivo se genera en el disco duro o SSD hoy en día y pone esto.

Código:
<MiInfo>
<TextBox>Texto y textos</TextBox>
<CheckBox>True</CheckBox>
<radioButton1>False</radioButton1>
<radioButton2>True</radioButton2>
</MiInfo>
Me funciona de maravilla para lo que es el XML y me lo recomendaron en este foro hace años, desde el 2.008.

Ahora, veo mucho con el santo JSON.

¿Es recomendable usar el JSON?

Si es cierto o no. Quiero hacer el mismo ejemplo de recuperar y guardar datos como se hizo el XML.

Saludos.
__________________
http://electronica-pic.blogspot.com....n-arduino.html Manuales de electrónica general, PIC y Arduino.

Última edición por Neftali [Germán.Estévez] fecha: 13-02-2023 a las 09:57:53. Razón: Corregir link a la imagen
Responder Con Cita