こんにちは、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" という単一のノードを選択します。そのために、「/Address/ZipCode」として xpath を使用しています (これは単なる例であるため、ここではフォーマットが間違っている可能性があります)。この場合、null を返す必要がありますが、代わりに "Element id=1" から ZipCode を返します。
どんな助けでもかなり..