How can I add elements to a .XML document?

1

I need to add nodes or elements to an .XML file saved in a location on my PC and I do not know how to do it ...

for example in my file I have:

<Empleado> 
  <Nombre>
     <PrimerNombre>Efrain</PrimerNombre>
     <SegundoNombre>Emilio</SegundoNombre>
  </Nombre>
</Empleado>

and I need to add you

<Apellido>
     <PrimerApellido>Mejias</PrimerApellido>
     <SegundoApellido>Castillo</SegundoApellido>
</Apellido>

to make it look like this:

<Empleado> 
  <Nombre>
     <PrimerNombre>Efrain</PrimerNombre>
     <SegundoNombre>Emilio</SegundoNombre>
  </Nombre>
  <Apellido>
     <PrimerApellido>Mejias</PrimerApellido>
     <SegundoApellido>Castillo</SegundoApellido>
  </Apellido>
</Empleado>

How can I do this?

    
asked by Efrain Mejias C 09.01.2018 в 20:49
source

1 answer

2
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load("XMLFile1.xml");
        XDocument doc = XDocument.Load("XMLFile1.xml");
        XElement root = new XElement("Apellido");
        root.Add(new XElement("PrimerApellido", "Mejias"));
        root.Add(new XElement("SegundoApellido", "Castillo"));

        doc.Element("Empleado").Add(root);
        doc.Save("XMLFile2.xml");
    
answered by 09.01.2018 / 22:09
source