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);
}
}
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.
– Thiagopsw