Read specific XML tags?

Asked

Viewed 1,974 times

1

I need to do the reading of tags specifics of that 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 the information tag Cnpj my code has a button(btnLerTag_Click) which 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 the tag CNPJ quietly if I leave mine XML thus:

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

But if the XML is in original form with those additional information within the tag Enviarloterpsenvio I can’t read the tag CNPJ.

[EDIT]

Code provided by Virgilio that solved the problem: I just made a few 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);
        }
    }

2 answers

0

  • Formatting is not wrong. This is an xml of an Nfse of the company system where I work. Still, I appreciate the attempt to help.

0


He owns the namespace (xmlns) contained in the file, then to solve this problem would:

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

  • Good morning Virgilio, Your code had some things wrong, but it’s working. I made some changes and it worked. I edited my question and added the corrected code. Just one more question, if I have more than one CNPJ tag in the XML file, how do I search for a specific CNPJ tag? Thank you so much for your help.

  • @Thiagopsw my code has no problems, including tested, now the adaptation to your you who does it? Another question doubts another question all right! the site works like this. if it is useful mark as answer to your question, if it is not all right.

  • I only cited a "problem" in your code due to the fact that it contains two objects with the same name (oNoList) of type 'Xmlnodelist'. Here the algorithm only worked after removing one of these statements.

  • Okay @Thiagopsw, vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.