viernes, 7 de octubre de 2011

Lectura de ficheros XML

Una buena opción para almacenar datos es la utilización de ficheros de tipo XML, que permiten una gestión de datos similar a la de una base de datos, pero de forma más sencilla aunque también más limitada.



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CÓDIGO LECTURA DE UN ATRIBUTO DE UN NODO EN UN FICHERO XML
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        TextBox1.Text =  persona.GetAttribute("nif") & Environment.NewLine


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CÓDIGO LECTURA DE UN TEXTO DE UN NODO EN UN FICHERO XML
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        TextBox1.Text =  persona.Item("nombre").InnerText 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CÓDIGO LECTURA DE TODOS LOS DATOS DE UN FICHERO XML
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Se declara una variable para almacenar el documento XML
        Dim documento_xml As New XmlDocument

        'Cargar en la variable el fichero
        documento_xml.Load("XMLFile1.xml")

        'Se define el nodo raiz
        Dim personas As XmlNode = documento_xml.DocumentElement

        For Each persona As XmlElement In personas.ChildNodes
            'Se muestran solo las personas mayores de edad
            If CInt(persona.Item("edad").InnerText) >= 18 Then
                TextBox1.Text = TextBox1.Text & persona.GetAttribute("nif") & Environment.NewLine
                TextBox1.Text = TextBox1.Text & persona.Item("nombre").InnerText & Environment.NewLine
                TextBox1.Text = TextBox1.Text & persona.Item("apellidos").InnerText & Environment.NewLine
                TextBox1.Text = TextBox1.Text & persona.Item("direccion").InnerText & Environment.NewLine
                TextBox1.Text = TextBox1.Text & persona.Item("cod_postal").InnerText & Environment.NewLine
                TextBox1.Text = TextBox1.Text & "- - - - - - - - -" & Environment.NewLine
            End If
        Next
    End Sub



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CONTENIDO DEL FICHERO XML
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

<?xml version="1.0" encoding="utf-8" ?>

<personas>
 
  <persona nif="12345678A">
    <edad> 25</edad>
    <nombre>Julia</nombre>
    <apellidos>Anilom Anul</apellidos>
    <direccion>C/ Rio lerez 8 </direccion>
    <cod_postal>36125</cod_postal>
  </persona>

  <persona nif="12547788T">
    <edad> 15</edad>
    <nombre>Ana Isabel</nombre>
    <apellidos>Martinez Leins</apellidos>
    <direccion>C/ Pazos s/n </direccion>
    <cod_postal>36998</cod_postal>
  </persona>

  <persona nif="12547758E">
    <edad> 65</edad>
    <nombre>Ana Isabel</nombre>
    <apellidos>Martinez Juste</apellidos>
    <direccion>C/ san andres s/n </direccion>
    <cod_postal>36004</cod_postal>
  </persona>
 
  <persona nif="12549958E">
    <edad> 5</edad>
    <nombre>Andrea</nombre>
    <apellidos>Martinez Martinez</apellidos>
    <direccion>C/ san andres s/n </direccion>
    <cod_postal>36004</cod_postal>
  </persona>

</personas>

1 comentario:

Related Posts Plugin for WordPress, Blogger...