Read Tags with the same name in XML

Asked

Viewed 478 times

1

I am with the following example 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 to be able to identify them as RPS, Provider and Policy Holder Cnpj.

I already managed to get the first XML Cnpj 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);
        }
}

Could someone give me some suggestion to solve this problem?

  • You want to know which one node belongs to the particular Cnpj?

  • Exactly that!

1 answer

1


Basically that would be it, if the layout of is the same as 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 is 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();
  • 1

    It worked perfectly. Thanks for the help Virgilio.

  • @Thiagopsw put another formal, can also help more!

Browser other questions tagged

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