Basics of XmlNode.SelectNodes?

Yes - that Jake. picture Yes - that Jake. · Mar 30, 2009 · Viewed 27.8k times · Source

I'm not sure why this isn't working.

I have an XmlNode in a known-format. It is:

<[setting-name]>
    <dictionary>
       <[block-of-xml-to-process]/>
       <[block-of-xml-to-process]/>
       <[block-of-xml-to-process]/>
    </dictionary>
</[setting-name]>

I have a reference to the node in a variable called pattern. I want an iterable collection of nodes, each of which is represented by a [block-of-xml-to-process] above. The name and structure of the blocks is unknown at this point. [Setting-name] is known.

This seems pretty straightforward. I can think of a half-dozen XPATH expressions that should point to the blocks. I've tried:

XmlNodeList kvpsList = pattern.SelectNodes(String.Format(@"/{0}/dictionary/*", _CollectionName));
XmlNodeList kvpsList = pattern.SelectNodes(String.Format(@"{0}/dictionary/*", _CollectionName));
XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary/*");
XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary");

But, I am apparently lacking some basic understanding of XPATH or some special trick of .SelectNodes because none of them work consistently.

What am I doing wrong?

Answer

marc_s picture marc_s · Mar 31, 2009

Have you tried removing the "@" from your XPath strings??

XmlNodeList kvpsList = pattern.SelectNodes("//dictionary");

That should work - does work for me on a daily basis :-)

Marc