Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我在XPathNodeIterator.Current.SelectSingleNode下搜索时,它只搜索整个xml文档_C#_Xpath_Xpathnavigator_Selectsinglenode_Xpathnodeiterator - Fatal编程技术网

C# 当我在XPathNodeIterator.Current.SelectSingleNode下搜索时,它只搜索整个xml文档

C# 当我在XPathNodeIterator.Current.SelectSingleNode下搜索时,它只搜索整个xml文档,c#,xpath,xpathnavigator,selectsinglenode,xpathnodeiterator,C#,Xpath,Xpathnavigator,Selectsinglenode,Xpathnodeiterator,您好,我正在使用XPathNodeIterator在XML文档下的XML节点列表中进行迭代。在迭代条件是否匹配时,我需要当前节点下的特定值。相反,SelectSingleNode从其他父节点返回节点…让我举一个例子: XPathNavigator SourceReqNav = // Load this using an xml; XmlNamespaceManager manager = new XmlNamespaceManager(SourceReqNav.NameTable); Sourc

您好,我正在使用XPathNodeIterator在XML文档下的XML节点列表中进行迭代。在迭代条件是否匹配时,我需要当前节点下的特定值。相反,SelectSingleNode从其他父节点返回节点…让我举一个例子:

XPathNavigator SourceReqNav = // Load this using an xml;
XmlNamespaceManager manager = new XmlNamespaceManager(SourceReqNav.NameTable);
SourceReqNav.MoveToRoot();
XPathNodeIterator nodeIterator = SourceReqNav.Select(parentPath, manager);

while (nodeIterator.MoveNext())
{
 //If condition matches at nodeIterator.Current node, Executing a partial xpath related to   
 //only that current node

XPathNavigator singleNode = nodeIterator.Current.SelectSingleNode(xpath, namespaceManager);

// Here at above line, It should return null as Child node doesn't exist but It 
// searches in whole xml and finding the matching one..even though the XPATH is kind of
// partial and applicable to only that node structure.
}
示例xml:

<RootNode>
 <Element id=1>
   <Address>
     <Name>      </Name>
     <ZipCode>456</ZipCode>
     <Street> </Street>
   </Address>
 </Element>
 <Element id=2>
 </Element>
 <Element id=3> </Element>
</RootNode>

456
比方说,我迭代到所有“元素”,并试图找到元素id=2的“地址”。 现在,在迭代时,假设我在“Element id=2”。当我找到节点时,我会说从当前节点(nodeIterator.current)选择一个名为“Zip”的节点。为此,我将xpath用作“/Address/ZipCode”(这里的格式可能错误,因为这只是一个示例)。在这种情况下,它应该返回null,但它从“Element id=1”返回ZipCode

任何帮助都是值得的。

由于时间限制(为了修复),我选择了更长的路线。 我没有直接在当前节点上搜索,而是从当前节点获取子树(类型:XmlReader),并使用它加载一个XPathDocument。然后从中创建了一个XPathNavigator。然后对其应用相同的部分XPath

nodeIterator.Current.SelectSingleNode(xpath, namespaceManager);
修改为:

XmlReader sourceReader = nodeIterator.Current.ReadSubtree();
XPathDocument doc = new XPathDocument(sourceReader);
XPathNavigator sourceNavigator = doc.CreateNavigator();
sourceNavigator.SelectSingleNode(xpath, namespaceManager);
如果有人有更好的答案,我还是想避免。希望这能帮助别人。

由于时间限制(为了修复),我选择了更长的路线。 我没有直接在当前节点上搜索,而是从当前节点获取子树(类型:XmlReader),并使用它加载一个XPathDocument。然后从中创建了一个XPathNavigator。然后对其应用相同的部分XPath

nodeIterator.Current.SelectSingleNode(xpath, namespaceManager);
修改为:

XmlReader sourceReader = nodeIterator.Current.ReadSubtree();
XPathDocument doc = new XPathDocument(sourceReader);
XPathNavigator sourceNavigator = doc.CreateNavigator();
sourceNavigator.SelectSingleNode(xpath, namespaceManager);

如果有人有更好的答案,我还是想避免。希望这能对其他人有所帮助。

我希望更多的人了解
XElement
如何使用LINQ。好吧,我确实使用LINQ转换XML。但这是更可定制的框架工作的一部分,用户可以使用XPATH(扩展)通过配置文件简单地更改映射。无论如何,那不是我想要的。我很感谢您花了这么多时间来研究。我希望更多的人了解
XElement
如何将LINQ与之结合使用。嗯,我确实使用LINQ转换XML。但这是更可定制的框架工作的一部分,用户可以使用XPATH(扩展)通过配置文件简单地更改映射。无论如何,那不是我想要的。我很感谢你花时间看一看。我知道这是一个非常古老的解决方案,但我也想要一个不创建另一个对象的解决方案。我知道这是一个非常古老的解决方案,但我也想要一个不创建另一个对象的解决方案。