Guest User

Untitled

a guest
Jan 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.02 KB | None | 0 0
  1.   private void Ingresar_Click(object sender, EventArgs e)
  2.         {
  3.             string nombre, apellido, rut, edad, institucion;
  4.             nombre = textBox1.Text.ToString();
  5.             apellido = textBox2.Text.ToString();
  6.             rut = textBox3.Text.ToString();
  7.             edad = textBox4.Text.ToString();
  8.             institucion = textBox5.Text.ToString();
  9.  
  10.             if (File.Exists("ArchivoAlumnos.xml"))
  11.             {
  12.                 string rut2, nombre2, apellido2, edad2, institucion2;
  13.                                
  14.                 XmlTextReader alumnos = new XmlTextReader("ArchivoAlumnos.xml");
  15.                 XmlTextWriter temporal = new XmlTextWriter("temporal.xml", null);
  16.  
  17.                 while(alumnos.ReadToFollowing("alumno"))
  18.                 {
  19.                     rut2 = alumnos.GetAttribute(0);
  20.                     alumnos.ReadToFollowing("Nombre");
  21.                     nombre2 = alumnos.ReadElementString();
  22.                     alumnos.ReadToFollowing("Apellido");
  23.                     apellido2 = alumnos.ReadElementString();
  24.                     alumnos.ReadToFollowing("Edad");
  25.                     edad2 = alumnos.ReadElementString();
  26.                     alumnos.ReadToFollowing("id_institucion");
  27.                     institucion2 = alumnos.ReadElementString();
  28.  
  29.                     if(String.Compare(rut,rut2) == 0){
  30.                         label7.Text = "Ya existe este alumno";
  31.                         break;
  32.                     }
  33.  
  34.                     if(string.Compare(rut,rut2)<0){
  35.                         temporal.WriteStartDocument();
  36.                         temporal.Formatting = Formatting.Indented;
  37.                         temporal.WriteStartElement("Alumnos");
  38.                         temporal.WriteStartElement("alumno");
  39.                         temporal.WriteAttributeString("rut",rut);
  40.                         temporal.WriteElementString("Nombre", nombre);
  41.                         temporal.WriteElementString("Apellido",apellido);
  42.                         temporal.WriteElementString("Edad",edad);
  43.                         temporal.WriteElementString("id_institucion", institucion);
  44.                         temporal.WriteEndElement();
  45.  
  46.                         while(alumnos.ReadToFollowing("alumno"))
  47.                         {
  48.  
  49.                             rut2 = alumnos.GetAttribute(0);
  50.                             alumnos.ReadToFollowing("Nombre");
  51.                             nombre2 = alumnos.ReadElementString();
  52.                             alumnos.ReadToFollowing("Apellido");
  53.                             apellido2 = alumnos.ReadElementString();
  54.                             alumnos.ReadToFollowing("Edad");
  55.                             edad2 = alumnos.ReadElementString();
  56.                             alumnos.ReadToFollowing("id_institucion");
  57.                             institucion2 = alumnos.ReadElementString();
  58.  
  59.                             temporal.WriteStartElement("alumno");
  60.                             temporal.WriteAttributeString("rut",rut2);
  61.                             temporal.WriteElementString("Nombre", nombre2);
  62.                             temporal.WriteElementString("Apellido",apellido2);
  63.                             temporal.WriteElementString("Edad",edad2);
  64.                             temporal.WriteElementString("id_institucion", institucion2);
  65.                             temporal.WriteEndElement();
  66.                         }
  67.  
  68.                     }
  69.  
  70.                     else if (string.Compare(rut,rut2)>0){
  71.  
  72.                         while(alumnos.ReadToFollowing("alumno"))
  73.                         {
  74.                                rut2 = alumnos.GetAttribute(0);
  75.                                alumnos.ReadToFollowing("Nombre");
  76.                                nombre2 = alumnos.ReadElementString();
  77.                                alumnos.ReadToFollowing("Apellido");
  78.                                apellido2 = alumnos.ReadElementString();
  79.                                alumnos.ReadToFollowing("Edad");
  80.                                edad2 = alumnos.ReadElementString();
  81.                                alumnos.ReadToFollowing("id_nstitucion");
  82.                                institucion2 = alumnos.ReadElementString();
  83.  
  84.                             if (string.Compare(rut,rut2)<0)
  85.                             {
  86.                                  temporal.WriteStartElement("Alumnos");
  87.                                  temporal.WriteStartElement("alumno");
  88.                                  temporal.WriteAttributeString("rut",rut);
  89.                                  temporal.WriteElementString("Nombre", nombre);
  90.                                  temporal.WriteElementString("Apellido",apellido);
  91.                                  temporal.WriteElementString("Edad",edad);
  92.                                  temporal.WriteElementString("id_institucion", institucion);
  93.                                  temporal.WriteEndElement();
  94.                             }
  95.  
  96.                             else
  97.                             {
  98.                                  temporal.WriteStartElement("Alumnos");
  99.                                  temporal.WriteStartElement("alumno");
  100.                                  temporal.WriteAttributeString("rut",rut2);
  101.                                  temporal.WriteElementString("Nombre", nombre2);
  102.                                  temporal.WriteElementString("Apellido",apellido2);
  103.                                  temporal.WriteElementString("Edad",edad2);
  104.                                  temporal.WriteElementString("id_institucion", institucion2);
  105.                                  temporal.WriteEndElement();
  106.                             }
  107.                                                
  108.                         }
  109.                    
  110.                     }
  111.                 }
  112.  
  113.                     alumnos.Close();
  114.                     temporal.Close();
  115.                     File.Replace("temporal.xml", "ArchivoAlumnos.xml", "ArchivoTemporal.xml");
  116.                     File.Delete("ArchivoTemporal.xml");
  117.                     label7.Text = "Alumno se ingreso Satisfactoriamente";
  118.             }  
  119.            
  120.  
  121.             else
  122.             {
  123.                 XmlTextWriter walumnos = new XmlTextWriter("ArchivoAlumnos.xml", null);
  124.                 walumnos.Formatting = Formatting.Indented;
  125.                 walumnos.WriteStartDocument();
  126.                 walumnos.WriteStartElement("Alumnos");
  127.                 walumnos.WriteStartElement("alumno");
  128.                 walumnos.WriteAttributeString("RUT", rut);
  129.                 walumnos.WriteElementString("Nombre", nombre);
  130.                 walumnos.WriteElementString("Apellido", apellido);
  131.                 walumnos.WriteElementString("Edad", edad);
  132.                 walumnos.WriteElementString("id_institucion", institucion);
  133.                 walumnos.WriteEndElement();
  134.                 walumnos.WriteEndElement();
  135.                 walumnos.WriteEndDocument();
  136.                 walumnos.Flush();
  137.                 walumnos.Close();
  138.         }
  139.        
  140.             myForm.Visible = true;
  141.             this.Close();
  142.         }
Add Comment
Please, Sign In to add comment