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);
}
}