To get the value of an element in XML document, I write something like this:
XmlNodeList nodo = documentoXml.GetElementsByTagName("Fecha");
This returns me a list of the elements with the specified name, and I can access their values by their position in the list. For example:
string Fecha = nodo[0].InnerXml;
But if the element has children like the Detail element, how can I get their values? for instance:
<?xml version="1.0" encoding="UTF-8"?>
-<FacturaElectronica xmlns="https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/facturaElectronica" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Clave>50628031800310140886100100001010000000003145592145</Clave>
<NumeroConsecutivo>00100001010000000003</NumeroConsecutivo>
<FechaEmision>2018-03-28T08:00:24-04:00</FechaEmision>
-<Emisor>
<Nombre>NEOTECNOLOGIAS SA</Nombre>
-<Identificacion>
<Tipo>02</Tipo>
<Numero>3101408861</Numero>
</Identificacion>
<NombreComercial>NEOTECNOLOGIAS SA</NombreComercial>
-<Ubicacion>
<Provincia>1</Provincia>
<Canton>02</Canton>
<Distrito>01</Distrito>
<Barrio>01</Barrio>
<OtrasSenas>CENTRO CORPORATIVO PLAZA ROBLE EDIFICIO LAS TERRAZAS 5TO PISO</OtrasSenas>
</Ubicacion>
-<Telefono>
<CodigoPais>506</CodigoPais>
<NumTelefono>40701540</NumTelefono>
</Telefono>
-<Fax>
<CodigoPais>506</CodigoPais>
<NumTelefono>40701540</NumTelefono>
</Fax>
<CorreoElectronico>[email protected]</CorreoElectronico>
</Emisor>
-<Receptor>
<Nombre>Dental Care</Nombre>
-<Identificacion>
<Tipo>02</Tipo>
<Numero>3001123208</Numero>
</Identificacion>
<NombreComercial>Dental Care</NombreComercial>
-<Ubicacion>
<Provincia>1</Provincia>
<Canton>01</Canton>
<Distrito>01</Distrito>
<Barrio>01</Barrio>
<OtrasSenas>Amon</OtrasSenas>
</Ubicacion>
-<Telefono>
<CodigoPais>506</CodigoPais
<NumTelefono>40701590</NumTelefono>
</Telefono>
-<Fax>
<CodigoPais>506</CodigoPais>
<NumTelefono>40701590</NumTelefono>
</Fax>
<CorreoElectronico>[email protected]</CorreoElectronico>
</Receptor>
</FacturaElectronica>
How can I get the values of Number and Reference children of the Detail node?
Well, based on your way of working, you recover the Detail node and within it you recover the nodes (which would be subnodes), and you specify which ones you want to recover.
I enclose a small code, I am not so much of C#, I stayed in VB, but I hope you understand the idea.
If you dare to use linq to xml you could use something like this
as you will see it is very simple to work the nodes and convert this to objects
One way to obtain the values of elements of an XML document is using XPATH syntax, in this way it can be accessed directly or randomly according to its position within the document...
I explain
For example, if there are several elements called Number in the document and we want to obtain the value of the second element of this type, we do the following: