Read Tags with the same name in XML

1

I have the following sample XML file:

<?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>     
    <QuantidadeRps>1</QuantidadeRps>
    <ListaRps>
        <Rps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                                                                                                                                                                                                                                                         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                                                                                                                                                                                                                                                                         xmlns="http://www.abrasf.org.br/nfse">
            <InfRps Id="Rps0716072000016300500787"> 
                <Prestador>
                    <Cnpj>07160720000222</Cnpj>                     
                </Prestador>
                <Tomador>
                    <IdentificacaoTomador>
                        <CpfCnpj>
                            <Cnpj>07160720000333</Cnpj>
                        </CpfCnpj>
                    </IdentificacaoTomador>
                </Tomador>
            </InfRps>               
        </Rps>
    </ListaRps>
</LoteRps>  

My goal is to obtain the information of each Cnpj TAG separately so that I can identify them as Cnpj of the RPS, the Provider and the Policyholder.

I've already managed to get the first Cnpj of XML with this code C#

private void btnLerTag_Click(object sender, EventArgs e)
    {

        //Busca o primeiro CNPJ do XML
        XmlDocument doc = new XmlDocument();
        string ArquivoXML = txtCaminhoXML.Text;
        doc.Load(ArquivoXML);
        XmlNode root = doc.DocumentElement;

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("n", "http://www.abrasf.org.br/nfse");            
        XmlNode node = root.SelectSingleNode("//n:Cnpj", nsmgr);            

        foreach (XmlNode oNo in node)
        {
            lstXML.Items.Add(node.InnerXml);
        }
}

Can anyone give me any suggestions to solve this problem?

    
asked by anonymous 15.03.2017 / 16:11

1 answer

1

Basically this would be if the layout of the is the same of the question:

XmlDocument doc = new XmlDocument();
string ArquivoXML = txtCaminhoXML.Text;
doc.Load(ArquivoXML);
XmlNode root = doc.DocumentElement;

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("n", "http://www.abrasf.org.br/nfse");

XmlNode nodeLoteRps = root.SelectSingleNode("//n:LoteRps", nsmgr)
    .SelectSingleNode("//n:Cnpj", nsmgr);

XmlNode nodeTomador = root.SelectSingleNode("//n:Tomador", nsmgr);

XmlNode nodePrestador = root.SelectSingleNode("//n:Prestador", nsmgr);

Console.WriteLine("LoteRPS: " + nodeLoteRps.InnerText);
Console.WriteLine("Prestador:" + nodePrestador.InnerText);
Console.WriteLine("Tomador: " + nodeTomador.InnerText);

There's also a way with :

XNamespace nsSys = "http://www.abrasf.org.br/nfse";
var result = (from c in XDocument.Load("d.xml").Descendants()
                .Elements(nsSys + "LoteRps")
              let a = c.Element(nsSys + "NumeroLote")
              let b = c.Element(nsSys + "Cnpj")
              let d = c.Element(nsSys + "QuantidadeRps")
              let e = c.Element(nsSys + "ListaRps")
                .Element(nsSys + "Rps")
                .Element(nsSys + "InfRps")
                .Element(nsSys + "Prestador")
                .Element(nsSys + "Cnpj")
              let f = c.Element(nsSys + "ListaRps")
              .Element(nsSys + "Rps")
              .Element(nsSys + "InfRps")
              .Element(nsSys + "Tomador")
              .Element(nsSys + "IdentificacaoTomador")
              .Element(nsSys + "CpfCnpj")
              .Element(nsSys + "Cnpj")
              select new
              {
                  NumeroLote = a?.Value,
                  Cnpj = b?.Value,
                  QuantidadeRps = d?.Value,
                  PrestadorCnpj = e?.Value,
                  TomadorCnpj = f?.Value
              })
              .FirstOrDefault();
    
15.03.2017 / 17:18