Hola a todos, hoy os voy a explicar como podemos escribir un fichero XML en C#.
En C# para el tema de XML, tenemos una clase para tratar XML, tanto como leer y escribir. Esta clase es XmlDocument.
El xml que vamos a crear es este:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
xml version="1.0" encoding="UTF-8" standalone="no"?> |
Lo primero es crear una instancia de XmlDocument:
|
1 |
XmlDocument doc = new XmlDocument(); |
El primer elemento que recomiendo crear es el raíz (antes incluso de la definición del documento), que en nuestro caso es concesionario.
|
1 2 |
XmlElement concesionario = doc.CreateElement("concesionario"); doc.AppendChild(concesionario); |
Ahora si vamos a crear la definición del documento XML, le pondremos versión 1 y codificación UTF-8. Lo insertamos antes del elemento padre (concesionario).
|
1 2 |
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.InsertBefore(declaration, concesionario); |
Ahora vamos a crear el elemento coches, en este caso lo insertamos dentro del elemento raiz concesionario.
|
1 2 |
XmlElement coches = doc.CreateElement("coches"); concesionario.AppendChild(coches); |
Después, vamos a meter elementos coche, lo vamos a hacer por pasos:
|
1 |
XmlElement coche = doc.CreateElement("coche"); |
Le añadimos un atributo matricula:
|
1 2 3 |
XmlAttribute matricula1 = doc.CreateAttribute("matricula"); matricula1.Value = "1111AAA"; coche.SetAttributeNode(matricula1); |
Añadimos los nodos hijos de coche:
|
1 2 3 4 5 6 7 8 9 10 |
XmlElement marca = doc.CreateElement("marca"); XmlText marcaText1 = doc.CreateTextNode("AUDI"); marca.AppendChild(marcaText1); XmlElement precio = doc.CreateElement("precio"); XmlText precioText1 = doc.CreateTextNode("30000"); precio.AppendChild(precioText1); coche.AppendChild(marca); coche.AppendChild(precio); |
Por ultimo añadimos el coche al elemento coches:
|
1 |
coches.AppendChild(coche); |
Hago lo mismo con el resto de coches:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
// Coche 2 XmlElement coche2 = doc.CreateElement("coche"); XmlAttribute matricula2 = doc.CreateAttribute("matricula"); matricula2.Value = "2222BBB"; coche2.SetAttributeNode(matricula2); XmlElement marca2 = doc.CreateElement("marca"); XmlText marcaText2 = doc.CreateTextNode("SEAT"); marca2.AppendChild(marcaText2); XmlElement precio2 = doc.CreateElement("precio"); XmlText precioText2 = doc.CreateTextNode("10000"); precio2.AppendChild(precioText2); coche2.AppendChild(marca2); coche2.AppendChild(precio2); coches.AppendChild(coche2); // Coche 3 XmlElement coche3 = doc.CreateElement("coche"); XmlAttribute matricula3 = doc.CreateAttribute("matricula"); matricula3.Value = "3333CCC"; coche3.SetAttributeNode(matricula3); XmlElement marca3 = doc.CreateElement("marca"); XmlText marcaText3 = doc.CreateTextNode("BMW"); marca3.AppendChild(marcaText3); XmlElement precio3 = doc.CreateElement("precio"); XmlText precioText3 = doc.CreateTextNode("20000"); precio3.AppendChild(precioText3); coche3.AppendChild(marca3); coche3.AppendChild(precio3); coches.AppendChild(coche3); // Coche 4 XmlElement coche4 = doc.CreateElement("coche"); XmlAttribute matricula4 = doc.CreateAttribute("matricula"); matricula4.Value = "4444DDD"; coche4.SetAttributeNode(matricula4); XmlElement marca4 = doc.CreateElement("marca"); XmlText marcaText4 = doc.CreateTextNode("TOYOTA"); marca4.AppendChild(marcaText4); XmlElement precio4 = doc.CreateElement("precio"); XmlText precioText4 = doc.CreateTextNode("10000"); precio4.AppendChild(precioText4); coche4.AppendChild(marca4); coche4.AppendChild(precio4); coches.AppendChild(coche4); |
Ya tenemos nuestro XML hecho, pero falta guardarlo en un fichero:
|
1 |
doc.Save("concesionario.xml"); |
Recordar que se guarda en bin/Debug.
Aqui el resultado:

Os dejo el ejemplo completo:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace XML2 { class Program { static void Main(string[] args) { XmlDocument doc = new XmlDocument(); XmlElement concesionario = doc.CreateElement("concesionario"); doc.AppendChild(concesionario); XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.InsertBefore(declaration, concesionario); XmlElement coches = doc.CreateElement("coches"); concesionario.AppendChild(coches); // coche 1 XmlElement coche = doc.CreateElement("coche"); XmlAttribute matricula1 = doc.CreateAttribute("matricula"); matricula1.Value = "1111AAA"; XmlElement marca = doc.CreateElement("marca"); XmlText marcaText1 = doc.CreateTextNode("AUDI"); marca.AppendChild(marcaText1); XmlElement precio = doc.CreateElement("precio"); XmlText precioText1 = doc.CreateTextNode("30000"); precio.AppendChild(precioText1); coche.SetAttributeNode(matricula1); coche.AppendChild(marca); coche.AppendChild(precio); coches.AppendChild(coche); // Coche 2 XmlElement coche2 = doc.CreateElement("coche"); XmlAttribute matricula2 = doc.CreateAttribute("matricula"); matricula2.Value = "2222BBB"; XmlElement marca2 = doc.CreateElement("marca"); XmlText marcaText2 = doc.CreateTextNode("SEAT"); marca2.AppendChild(marcaText2); XmlElement precio2 = doc.CreateElement("precio"); XmlText precioText2 = doc.CreateTextNode("10000"); precio2.AppendChild(precioText2); coche2.SetAttributeNode(matricula2); coche2.AppendChild(marca2); coche2.AppendChild(precio2); coches.AppendChild(coche2); // Coche 3 XmlElement coche3 = doc.CreateElement("coche"); XmlAttribute matricula3 = doc.CreateAttribute("matricula"); matricula3.Value = "3333CCC"; XmlElement marca3 = doc.CreateElement("marca"); XmlText marcaText3 = doc.CreateTextNode("BMW"); marca3.AppendChild(marcaText3); XmlElement precio3 = doc.CreateElement("precio"); XmlText precioText3 = doc.CreateTextNode("20000"); precio3.AppendChild(precioText3); coche3.SetAttributeNode(matricula3); coche3.AppendChild(marca3); coche3.AppendChild(precio3); coches.AppendChild(coche3); // Coche 4 XmlElement coche4 = doc.CreateElement("coche"); XmlAttribute matricula4 = doc.CreateAttribute("matricula"); matricula4.Value = "4444DDD"; XmlElement marca4 = doc.CreateElement("marca"); XmlText marcaText4 = doc.CreateTextNode("TOYOTA"); marca4.AppendChild(marcaText4); XmlElement precio4 = doc.CreateElement("precio"); XmlText precioText4 = doc.CreateTextNode("10000"); precio4.AppendChild(precioText4); coche4.SetAttributeNode(matricula4); coche4.AppendChild(marca4); coche4.AppendChild(precio4); coches.AppendChild(coche4); doc.Save("concesionario.xml"); Console.WriteLine("Se ha escrito el XML"); Console.ReadLine(); } } } |
Os dejo un video explicándolo paso a paso:
Espero que os sea de ayuda. Si tenéis dudas, preguntad. Estamos para ayudarte.
Deja una respuesta