您好我正在使用 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>
假设,我正在迭代所有“元素”并试图找到元素 id=2 的“地址”。现在,在迭代时假设我在“Element id=2”。当我找到节点时,我会说从当前节点 (nodeIterator.Current) 中选择一个名为“Zip”的节点。为此,我将 xpath 用作“/Address/ZipCode”(此处的格式可能是错误的,因为这只是示例)。在这种情况下,它应该返回 null,而是从“Element id=1”返回 ZipCode
任何帮助将是可观的..