Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void Ingresar_Click(object sender, EventArgs e)
- {
- string nombre, apellido, rut, edad, institucion;
- nombre = textBox1.Text.ToString();
- apellido = textBox2.Text.ToString();
- rut = textBox3.Text.ToString();
- edad = textBox4.Text.ToString();
- institucion = textBox5.Text.ToString();
- if (File.Exists("ArchivoAlumnos.xml"))
- {
- string rut2, nombre2, apellido2, edad2, institucion2;
- XmlTextReader alumnos = new XmlTextReader("ArchivoAlumnos.xml");
- XmlTextWriter temporal = new XmlTextWriter("temporal.xml", null);
- while(alumnos.ReadToFollowing("alumno"))
- {
- rut2 = alumnos.GetAttribute(0);
- alumnos.ReadToFollowing("Nombre");
- nombre2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("Apellido");
- apellido2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("Edad");
- edad2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("id_institucion");
- institucion2 = alumnos.ReadElementString();
- if(String.Compare(rut,rut2) == 0){
- label7.Text = "Ya existe este alumno";
- break;
- }
- if(string.Compare(rut,rut2)<0){
- temporal.WriteStartDocument();
- temporal.Formatting = Formatting.Indented;
- temporal.WriteStartElement("Alumnos");
- temporal.WriteStartElement("alumno");
- temporal.WriteAttributeString("rut",rut);
- temporal.WriteElementString("Nombre", nombre);
- temporal.WriteElementString("Apellido",apellido);
- temporal.WriteElementString("Edad",edad);
- temporal.WriteElementString("id_institucion", institucion);
- temporal.WriteEndElement();
- while(alumnos.ReadToFollowing("alumno"))
- {
- rut2 = alumnos.GetAttribute(0);
- alumnos.ReadToFollowing("Nombre");
- nombre2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("Apellido");
- apellido2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("Edad");
- edad2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("id_institucion");
- institucion2 = alumnos.ReadElementString();
- temporal.WriteStartElement("alumno");
- temporal.WriteAttributeString("rut",rut2);
- temporal.WriteElementString("Nombre", nombre2);
- temporal.WriteElementString("Apellido",apellido2);
- temporal.WriteElementString("Edad",edad2);
- temporal.WriteElementString("id_institucion", institucion2);
- temporal.WriteEndElement();
- }
- }
- else if (string.Compare(rut,rut2)>0){
- while(alumnos.ReadToFollowing("alumno"))
- {
- rut2 = alumnos.GetAttribute(0);
- alumnos.ReadToFollowing("Nombre");
- nombre2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("Apellido");
- apellido2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("Edad");
- edad2 = alumnos.ReadElementString();
- alumnos.ReadToFollowing("id_nstitucion");
- institucion2 = alumnos.ReadElementString();
- if (string.Compare(rut,rut2)<0)
- {
- temporal.WriteStartElement("Alumnos");
- temporal.WriteStartElement("alumno");
- temporal.WriteAttributeString("rut",rut);
- temporal.WriteElementString("Nombre", nombre);
- temporal.WriteElementString("Apellido",apellido);
- temporal.WriteElementString("Edad",edad);
- temporal.WriteElementString("id_institucion", institucion);
- temporal.WriteEndElement();
- }
- else
- {
- temporal.WriteStartElement("Alumnos");
- temporal.WriteStartElement("alumno");
- temporal.WriteAttributeString("rut",rut2);
- temporal.WriteElementString("Nombre", nombre2);
- temporal.WriteElementString("Apellido",apellido2);
- temporal.WriteElementString("Edad",edad2);
- temporal.WriteElementString("id_institucion", institucion2);
- temporal.WriteEndElement();
- }
- }
- }
- }
- alumnos.Close();
- temporal.Close();
- File.Replace("temporal.xml", "ArchivoAlumnos.xml", "ArchivoTemporal.xml");
- File.Delete("ArchivoTemporal.xml");
- label7.Text = "Alumno se ingreso Satisfactoriamente";
- }
- else
- {
- XmlTextWriter walumnos = new XmlTextWriter("ArchivoAlumnos.xml", null);
- walumnos.Formatting = Formatting.Indented;
- walumnos.WriteStartDocument();
- walumnos.WriteStartElement("Alumnos");
- walumnos.WriteStartElement("alumno");
- walumnos.WriteAttributeString("RUT", rut);
- walumnos.WriteElementString("Nombre", nombre);
- walumnos.WriteElementString("Apellido", apellido);
- walumnos.WriteElementString("Edad", edad);
- walumnos.WriteElementString("id_institucion", institucion);
- walumnos.WriteEndElement();
- walumnos.WriteEndElement();
- walumnos.WriteEndDocument();
- walumnos.Flush();
- walumnos.Close();
- }
- myForm.Visible = true;
- this.Close();
- }
Add Comment
Please, Sign In to add comment