与使用XPATH命名空间中选择一个节点的XML问题节点、问题、空间、XPATH

由网友(想念是会呼吸的痛)分享简介:我有以下XPATH行:// DET [@ nItem域=1] /生产/ cProd 这成功地使用选择所需的节点 XPath的展台,它会自动识别名称空间,您可以定义要选择其中的命名空间。当我指定在C#中的命名空间与以下XPath code:http://www.portalfiscal.inf.br/nfe//det [...

我有以下XPATH行:

  

// DET [@ nItem域=1] /生产/ cProd

这成功地使用选择所需的节点 XPath的展台,它会自动识别名称空间,您可以定义要选择其中的命名空间。

当我指定在C#中的命名空间与以下XPath code:

 http://www.portalfiscal.inf.br/nfe//det [@ nItem域= 1 ] /生产/ cProd
 
Kettle 5 获取 Web 数据

它给了我一个XPathException的:

  

类型的未处理的异常   System.Xml.XPath.XPathException   发生在system.xml.dll的附加   信息:   http://www.portalfiscal.inf.br/nfe//det[@nItem="1"]/prod/cProd   有一个无效的标准名称。

(正如你所看到的,这不是任何转义字符或任何东西,因为它给了我什么,我试图达到的除外)

我该如何正确地选择此节点提供我所知道使用XPath命名空间?

- - 整条生产线,我尝试读取节点:

doc.XPathSelectElement("http://www.portalfiscal.inf.br/nfe//det[@nItem="1"]/prod/cProd").Value;

和不必要的东西XML切出:

 < XML版本=1.0编码=UTF-8&GT?;
< enviNFe的xmlns =htt​​p://www.portalfiscal.inf.br/nfeversao =1.10>
&其中; idLote→1&其中; / idLote>
< NFE>
< infNFe versao =1.10ID =NFe31100118583682000178550010000077778397333128>
&所述; DET nItem域=1>
<督促>
&其中; cProd&GT 111所述; / cProd>
< / PROD>
< / DET>
< / infNFe>
< / NFE>
< / enviNFe>
 

(不必要的东西切出不应该是一个问题,因为XPath的展台给我带来的节点没有任何问题)

解决方案

既然你不向我们展示,无论是XML文档,也不是C#code你有,我只能猜测你在做什么.. ..

OK,看来你正在使用LINQ到XML,所以后来用这个code在这里片段:

  //创建并加载XML阅读器
XmlReader的读者= XmlReader.Create(新的FileStream(@D. 的test.xml,FileAccess.Read));

//获取根元素
的XElement根= XElement.Load(读卡器);


//创建XML命名空间管理实例
XmlNamespaceManager的nsmgr =新的XmlNamespaceManager(reader.NameTable);

//添加您的命名空间的经理,并给它一个preFIX
nsmgr.AddNamespace(NS,http://www.portalfiscal.inf.br/nfe);

的XElement节点= root.XPathSelectElement(// NS:DET [@ nItem域=1] / NS:PROD / NS:cProd,nsmgr);
.......
 

类似的规定。你基本上要建立某种形式的XML命名空间,给它一个preFIX,然后使用preFIX在你的XPath EX pression - 而不是整个空间 - 只是preFIX

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[@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:

// Create and load XML reader
XmlReader reader = XmlReader.Create(new FileStream(@"D.test.xml", FileAccess.Read));

// get the root element    
XElement root = XElement.Load(reader);


// create instance of XML namespace manager
XmlNamespaceManager nsmgr = new XmlNamespaceManager(reader.NameTable);

// add your namespace to the manager and give it a prefix
nsmgr.AddNamespace("ns", "http://www.portalfiscal.inf.br/nfe");

XElement node = root.XPathSelectElement("//ns:det[@nItem="1"]/ns:prod/ns:cProd", nsmgr);
.......

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.

阅读全文

相关推荐

最新文章