Problem when selecting node value in XML document

1

I need to assign the value of an element of an XML document to a variable, specifically the value of the name element Clave , I wrote this but it does not work since it does not find the node because the function does not return any value. ..what am I doing wrong ... ??

  public string ValoresCamposXml(XmlDocument documentoXml)
  {
      string valores = string.Empty;
      XmlNode nodo = documentoXml.SelectSingleNode("//Clave");
      valores = nodo.InnerText;
      return valores;
  }

this is the xml document:

<?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>50627031800310140886100100001010000000003112465302</Clave>

<NumeroConsecutivo>00100001010000000003</NumeroConsecutivo>
<FechaEmision>2018-03-27T08:00:20-04:00</FechaEmision> 
-<Emisor>
<Nombre>TECNOLOGIAS SA</Nombre>
-<Identificacion>
<Tipo>02</Tipo>
<Numero>310rty14088uyt61</Numero>
</Identificacion>
<NombreComercial>TECNOLOGIAS 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>
</FacturaElectronica>
    
asked by Efrain Mejias C 28.03.2018 в 02:22
source

2 answers

2

If you dare to use linq to xml it is very simple to access the value of the key.

Something like this

using System;
using System.Xml;
using System.Linq;
using System.Xml.Linq;

public class Program
{
    public static void Main()
    {

        string xml = @"<?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>50627031800310140886100100001010000000003112465302</Clave>

                        <NumeroConsecutivo>00100001010000000003</NumeroConsecutivo>
                        <FechaEmision>2018-03-27T08:00:20-04:00</FechaEmision> 
                        <Emisor>
                        <Nombre>TECNOLOGIAS SA</Nombre>
                        <Identificacion>
                        <Tipo>02</Tipo>
                        <Numero>310rty14088uyt61</Numero>
                        </Identificacion>
                        <NombreComercial>TECNOLOGIAS 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>
                        </FacturaElectronica>";

        var doc = XDocument.Parse(xml); 


        XNamespace xmlns = "https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/facturaElectronica"; 

        var clave = doc.Descendants(xmlns + "Clave").First().Value;


        Console.WriteLine(clave);
    }
}
    
answered by 28.03.2018 в 04:49
1

I was able to obtain the value of the elements of an xml using this line of code:

XmlNodeList nodo = documentoXml.GetElementsByTagName("NombreDelNodo");

This returns a list of the elements with the specified name, and I can access their values by the position they occupy in the list..for example:

string valor = nodo[0].InnerXml;

A more detailed example appears here: link

    
answered by 28.03.2018 в 20:50