Read specific XML tags?

1

I need to read tags specific to this file XML :

<?xml version="1.0" encoding="UTF-8"?>
<EnviarLoteRpsEnvio xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns="http://www.abrasf.org.br/nfse">
<LoteRps Id="Lote4">
    <NumeroLote>4</NumeroLote>
    <Cnpj>07160720000111</Cnpj>
    <InscricaoMunicipal>00500787</InscricaoMunicipal>
    <QuantidadeRps>1</QuantidadeRps>        
</LoteRps>  

For example: I am using this code to read information tag Cnpj my code has a button ( btnLerTag_Click ) that serves to feed a Listbox ( lstXML ).

using System;
using System.Collections;
using System.Windows.Forms;
using System.Xml;

namespace LendoXML
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnLerTag_Click(object sender, EventArgs e)
    {            
        XmlDocument oXML = new XmlDocument();
        XmlNodeList oNoLista = default(XmlNodeList);                
        string ArquivoXML = txtCaminhoXML.Text;                
        oXML.Load(ArquivoXML);
        oNoLista = oXML.SelectNodes("/EnviarLoteRpsEnvio/LoteRps/Cnpj");

        foreach (XmlNode oNo in oNoLista)
        {
            lstXML.Items.Add(oNo.ChildNodes.Item(0).InnerText);
        }
    }
  }
}

I can read tag CNPJ quietly if I leave my XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<EnviarLoteRpsEnvio>
  <LoteRps Id="Lote4">
    <NumeroLote>4</NumeroLote>

But if the XML is in the original form with that additional information within tag SendLotRpsSend I can not read tag CNPJ .

[EDIT]

Code provided by Virgilio that solved the problem: I just made some changes to it

 private void btnLerTag_Click(object sender, EventArgs e)
    {            
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("n", "http://www.abrasf.org.br/nfse");

        XmlDocument oXML = new XmlDocument();
        string ArquivoXML = txtCaminhoXML.Text;           
        oXML.Load(ArquivoXML);
        XmlNode root = oXML.DocumentElement;            
        XmlNodeList oNoLista = root.SelectNodes("//n:Cnpj", nsmgr);

        foreach (XmlNode oNo in oNoLista)
        {
            lstXML.Items.Add(oNo.ChildNodes.Item(0).InnerText);
        }
    }
    
asked by anonymous 14.03.2017 / 21:13

2 answers

0

It has the namespace ( xmlns ) contained in the file, so to solve this problem would be:

private void btnLerTag_Click(object sender, EventArgs e)
{            
    //declaração do namespace      
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
    nsmgr.AddNamespace("n", "http://www.abrasf.org.br/nfse");        

    XmlDocument oXML = new XmlDocument();
    string ArquivoXML = txtCaminhoXML.Text;                
    oXML.Load(ArquivoXML);
    XmlNode root = oXML.DocumentElement;             
    XmlNodeList oNoLista = root.SelectNodes("//n:Cnpj", nsmgr);

    foreach (XmlNode oNo in oNoLista)
    {
        lstXML.Items.Add(oNo.ChildNodes.Item(0).InnerText);
    }
}

References

15.03.2017 / 01:37
0

Hello, I think the formatting of your xml is wrong. Check out this site that explains the structure of an xml file: link .

    
15.03.2017 / 00:07