与使用XPATH命名空间中选择一个节点的XML问题 [英] Problems in selecting a node in XML with Namespace using XPATH

查看:220
本文介绍了与使用XPATH命名空间中选择一个节点的XML问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XPATH行:

  

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

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

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

 http://www.portalfiscal.inf.br/nfe//det [@ nItem域= \1 \] /生产/ cProd
 

它给了我一个XPathException的:

  

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

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

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

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

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

和不必要的东西XML切出:

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

(不必要的东西切出不应该是一个问题,因为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 <。 / P>

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.

这篇关于与使用XPATH命名空间中选择一个节点的XML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆