I have the following XPATH line:
//det[@nItem=”1″]/prod/cProd
That successfully selects the desired node using XPath Visualizer, where it identifies automatically the namespace, and you define in which namespace you want to select.
When I specify the namespace in C# with the following XPATH code:
"http://www.portalfiscal.inf.br/nfe//det[@nItem=\"1\"]/prod/cProd"
it gives me an XPathException:
An unhandled exception of type
‘System.Xml.XPath.XPathException’
occurred in System.Xml.dll Additional
information:
‘http://www.portalfiscal.inf.br/nfe//det%5B@nItem=“1”]/prod/cProd’
has an invalid qualified name.
(as you can see, it’s not any escape character or anything, since it gives me what i’ve tried to reach in the exception)
How do I properly select this node providing that I know the namespace with XPath ?
–[EDIT]–
The complete line where I try to read the node:
doc.XPathSelectElement("http://www.portalfiscal.inf.br/nfe//det[@nItem=\"1\"]/prod/cProd").Value;
And the XML with unnecessary things cut out:
<?xml version="1.0" encoding="utf-8"?>
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.10">
<idLote>1</idLote>
<NFe>
<infNFe versao="1.10" Id="NFe31100118583682000178550010000077778397333128">
<det nItem="1">
<prod>
<cProd>111</cProd>
</prod>
</det>
</infNFe>
</NFe>
</enviNFe>
(The unnecessary things cut out should not be a problem, since XPath Visualizer brought me the node with no problems at all)
Since you’re not showing us neither the XML document, nor the C# code you have, I can only guess what you’re doing….
OK, seems you’re using Linq-to-XML, so then use this code snippet here:
Something along those lines. You basically have to create a XML namespace of some sort, give it a prefix, and then use that prefix in your XPath expression – not the whole namespace – just the prefix.