Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Meta en 22 Mayo 2009, 13:33 pm



Título: Guardar en XML.
Publicado por: Meta en 22 Mayo 2009, 13:33 pm
Hola:

Tengo un pequeño código. Se trata de cambiar los valores de Location y Size al pulsar el mismo botón.

Lo que no se hacer y quiero aprender, es saber como guardar los valores en un XML.

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Xml;
  11.  
  12. namespace Tamaño_Boton
  13. {
  14.    public partial class Form1 : Form
  15.    {
  16.        public Form1()
  17.        {
  18.            InitializeComponent();
  19.        }
  20.  
  21.        private void button1_Click(object sender, EventArgs e)
  22.        {
  23.            button1.Location = new Point(52, 12);
  24.            button1.Size = new Size(75, 65);
  25.        }
  26.  
  27.        private void button2_Click(object sender, EventArgs e)
  28.        {
  29.            try
  30.            {
  31.                XmlWriter w = XmlWriter.Create("Config.xml");
  32.                w.WriteStartElement("Form1");
  33.                // Código aquí.
  34.                w.WriteEndElement();
  35.                w.Close();
  36.            }
  37.            catch (IOException)
  38.            {
  39.                // bla, bla, bla...
  40.            }
  41.        }
  42.  
  43.        private void Form1_Load(object sender, EventArgs e)
  44.        {
  45.            try
  46.            {
  47.                XmlReader r = XmlReader.Create("Config.xml");
  48.                r.ReadStartElement("Form1");
  49.                // Código aquí.
  50.                r.ReadEndElement();
  51.                r.Close();
  52.            }
  53.            catch
  54.            {
  55.                // No se encuentra el archivo.
  56.            }
  57.        }
  58.    }
  59. }
  60.  


Saludo.


Título: Re: Guardar en XML.
Publicado por: Jorgitoh en 24 Mayo 2009, 01:31 am
Hola buenas noches. Mira ahora se me complica postearte una ayuda xq toi apunto de salir, asi que si me aguantas hasta el lunes te lo respondo.

Lo que si me gustaria saber es lo siguiente:

. Es necesario guardar los valores en XML?. Es decir, si el fin de la aplicacion es que al abrir el form tome los parametros que tenia antes podes usar las Settings ( no me acuerdo el nombre correcto ) que tiene .Net para guardar informacion dentro del proyecto.

. Ahora bien, no me queda claro la linea "w.WriteStartElement("Form1");". Vos del Form, que necesitas guardar? Solo la Location y Size o todos los atributos del form?. Si solo necesitas guardar esas dos variables podrias hacer que el XML quede algo asi:

< form >
< size valueX = 123 valueY = 123 >
< location valueX = 123 valueY = 123 >
< /form>

Si asi es como te gustaria que se guarden no creo que sea mucho problema resolverlo, fijate de responderme cuanto antes asi veo una solucion facil de comprender y explicar.

Saludos.


Título: Re: Guardar en XML.
Publicado por: Pablo Videla en 24 Mayo 2009, 01:47 am
Esto es un ejemplo de como guardar datos en un xml ... este codigo es mio , lo saque de una tarea que estoy haciendo xD y como es poco codigo , te sera facil interpretarlo

 

Código
  1. protected void btAgregarPelicula_Click(object sender, EventArgs e)
  2.    {
  3.  
  4.        string nombre = txtNombrePelicula.Text;
  5.        int minutos = int.Parse(txtMinutosPelicula.Text);
  6.        Boolean estreno;
  7.        string estreno2 = (string)cbEstreno.SelectedValue;
  8.        if (estreno2 == "si")
  9.        {
  10.            estreno = true;
  11.        }
  12.        else
  13.        {
  14.  
  15.            estreno = false;
  16.        }
  17.  
  18.  
  19.        Peliculas movie = new Peliculas(nombre, minutos, estreno);
  20.        //   peliculas.Add(movie);
  21.        Contenido.listado.getPeliculas().Add(movie);
  22.        // agregamos una pelicula
  23.        //  lbEstado.Text = "Ha sido agregada la Pelicula : "+movie.Nombre;
  24.        foreach (Peliculas a in Contenido.listado.getPeliculas())
  25.        {
  26.            lbEstado.Text = " La ultima pelicula agregada es " + a.Nombre;
  27.        }
  28.  
  29.  
  30.  
  31.        //*********CREACION DEL XML **********
  32.        XmlDocument doc = new XmlDocument();
  33.        XmlNode nodo;
  34.        XmlAttribute atributo;
  35.        IEnumerator iterador = Contenido.listado.getPeliculas().GetEnumerator();
  36.        // CREAMOS EL ARCHIVO XML
  37.        string archivo = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Peliculas.xml";
  38.  
  39.        nodo = doc.CreateElement("listadepeliculas");// CREAMOS LA ETIQUETA PRINCIPAL
  40.        doc.AppendChild(nodo);
  41.        while (iterador.MoveNext())
  42.        {
  43.  
  44.            // CREANDO LOS ATRIBUTOS DEL XML
  45.            una_pelicula = (Peliculas)iterador.Current;
  46.            nodo = doc.CreateElement("pelicula");
  47.  
  48.            atributo = doc.CreateAttribute("nombre");
  49.            atributo.InnerText = una_pelicula.Nombre.ToString();
  50.            nodo.Attributes.Append(atributo);
  51.  
  52.            atributo = doc.CreateAttribute("duracion");
  53.            atributo.InnerText = una_pelicula.Minutos.ToString();
  54.            nodo.Attributes.Append(atributo);
  55.  
  56.            atributo = doc.CreateAttribute("estreno");
  57.            atributo.InnerText = una_pelicula.Estreno.ToString();
  58.            nodo.Attributes.Append(atributo);
  59.  
  60.            //atributo = doc.CreateAttribute("nota2");
  61.            ///atributo.InnerText = un_alumno.Nota2.ToString();
  62.            //nodo.Attributes.Append(atributo);
  63.            doc.DocumentElement.AppendChild(nodo);
  64.            doc.Save(archivo);
  65.            //btVerXML.Enabled = true;
  66.          //  btResumenXML.Enabled = true;
  67.  
  68.        }
  69.    }
  70.  
  71.