Advertisement
Guest User

GuardaXML

a guest
May 23rd, 2010
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Xml;
  6.  
  7. namespace YParaHoy
  8. {
  9.     class GuardaXML
  10.     {
  11.         public void setCosas(String nombre, String detalle,
  12.             String Fecha, Int16 grado)
  13.         {
  14.             this.ScribeXML(nombre, detalle, Fecha, grado);
  15.         }
  16.  
  17.         private void MandaError(string str)
  18.         {
  19.             MessageBox.Show("" + str + "");
  20.         }
  21.        
  22.         private void ScribeXML(String nombre, String detalle,
  23.             String Fecha, Int16 grado)
  24.         {
  25.             try
  26.             {
  27.                 String filename = "cosasAhacer.xml";
  28.                 XmlDocument xmlDoc = new XmlDocument();
  29.  
  30.                 try
  31.                 {
  32.                     xmlDoc.Load(filename);
  33.                 }
  34.                 catch (System.IO.FileNotFoundException)
  35.                 {
  36.                     XmlTextWriter scribe = new XmlTextWriter(filename,
  37.                         System.Text.Encoding.UTF8);
  38.                     scribe.Formatting = Formatting.Indented;
  39.                     scribe.WriteProcessingInstruction("xml",
  40.                         "version='1.0' encoding='UTF-8'");
  41.                     scribe.WriteStartElement("Cosiacos");
  42.                     scribe.Close();
  43.                     xmlDoc.Load(filename);
  44.                 }
  45.                 XmlNode root = xmlDoc.DocumentElement;
  46.                 XmlElement Actividad = xmlDoc.CreateElement("Actividad");
  47.                 XmlElement Nombre = xmlDoc.CreateElement("Nombre");
  48.                 XmlElement Date = xmlDoc.CreateElement("Fecha");
  49.                 XmlElement Grado = xmlDoc.CreateElement("Grado");
  50.                 XmlElement Estado = xmlDoc.CreateElement("Estado");
  51.                 XmlElement Detalle = xmlDoc.CreateElement("Detalle");
  52.                 XmlText txtNom=xmlDoc.CreateTextNode(nombre),
  53.                     txtFecha=xmlDoc.CreateTextNode(Fecha),
  54.                     txtGrado=xmlDoc.CreateTextNode(grado.ToString()),
  55.                     txtEst=xmlDoc.CreateTextNode("Por Cumplir"),
  56.                     txtDate = xmlDoc.CreateTextNode(detalle);
  57.  
  58.                 root.AppendChild(Actividad);
  59.  
  60.                 Actividad.AppendChild(Nombre);
  61.                 Nombre.AppendChild(txtNom);
  62.  
  63.                 Actividad.AppendChild(Date);
  64.                 Date.AppendChild(txtFecha);
  65.  
  66.                 Actividad.AppendChild(Grado);
  67.                 Grado.AppendChild(txtGrado);
  68.  
  69.                 Actividad.AppendChild(Estado);
  70.                 Estado.AppendChild(txtEst);
  71.  
  72.                 Actividad.AppendChild(Detalle);
  73.                 Detalle.AppendChild(txtDate);
  74.  
  75.                 xmlDoc.Save(filename);
  76.                 MessageBox.Show("Todo bien ;)", "Listoco");
  77.             }
  78.             catch (Exception ex)
  79.             {
  80.                 MandaError(ex.ToString());
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement