XML selectnode不起作用

时间:2013-12-03 13:53:31

标签: c# .net xml

我有以下XML并希望获得下面的标记值chNFe代码并且无法捕获他。始终为零。

var doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.LoadXml(_documento.DocumentoAtual.ToString());

var temp = doc.SelectSingleNode("envEvento");

foreach (XmlNode nodePrincipal in doc.SelectNodes("envEvento/evento/infEvento"))
{
    var tipo = nodePrincipal.SelectSingleNode("chNFe").InnerText;
}

XML文件:

<?xml version="1.0" encoding="utf-8" ?> 
- <envEvento versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe">
<idLote>1</idLote> 
- <evento versao="1.00">
- <infEvento Id="ID1101115113128096780500014865100000000033100010026101">
  <cOrgao>42</cOrgao> 
  <tpAmb>2</tpAmb> 
  <CNPJ>80967805000148</CNPJ> 
  <chNFe>51131280967805000148651000000000331000100261</chNFe> 
  <dhEvento>2013-12-03T09:54:44-03:00</dhEvento> 
  <tpEvento>110111</tpEvento> 
  <nSeqEvento>1</nSeqEvento> 
  <verEvento>1.00</verEvento> 
- <detEvento versao="1.00">
  <descEvento>Cancelamento</descEvento> 
  <nProt>151130000040738</nProt> 
  <xJust>Cancelamento de nota</xJust> 
  </detEvento>
  </infEvento>
- <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
- <SignedInfo>
  <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> 
  <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> 
- <Reference URI="#ID1101115113128096780500014865100000000033100010026101">
- <Transforms>
  <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
  <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> 
  </Transforms>
  <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
  <DigestValue>7lcMMEETJPn6plzSmpSuodbBDx8=</DigestValue> 
  </Reference>
  </SignedInfo>
  <SignatureValue>KJVQK7iHS18mgkXkUGJJsVr6yL2yl39nQjw8A9XPn7UhVhnv2q0jjRifs6r4IdBxpCZMifqbkPHpxS+1tVTQGUTmTcGxOYJIBed37c3vuy88yzpv7465UgV8iA16KzUezGrSMiaItOrWFe0nzjOumD1tHJx85awGNSoPtEucqbRS8ofV4I0/7CW/ewV42reoW4miIci5ft8zzPA9hE4Mf934mHfAPgdJqrtVxypPgok+Tl6ED7PxfKHJyncnw78EECbZzBrlFS3bM4wVKp+LEJNzC/aUYO2BtDn2NKRhxXR1vPM/x47sZd1XQZUXRIqsNFTMB59fgCgeLunoVLif+w==</SignatureValue> 
- <KeyInfo>
- <X509Data>
  <X509Certificate>/X509Certificate> 
  </X509Data>
  </KeyInfo>
  </Signature>
  </evento>
  </envEvento>

1 个答案:

答案 0 :(得分:1)

编辑问题是您在文档中指定了命名空间:

XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("nspace", "http://www.portalfiscal.inf.br/nfe");

foreach (XmlNode nodePrincipal in doc.SelectNodes("//nspace:envEvento//nspace:evento//nspace:infEvento", ns))
{
    var tipo = nodePrincipal.SelectSingleNode("nspace:chNFe", ns).InnerText;
}