Basics of XmlNode.SelectNodes?

28,229

Solution 1

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

Solution 2

Have you tried:

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

OR

XmlNodeList kvpsList = pattern.SelectNodes(@"/[setting-name]/dictionary:child");

Pretty much gets the children of "dictionary" If that doesnt work, does the actual call to dictionary work?

Share:
28,229
Yes - that Jake.
Author by

Yes - that Jake.

A polyglot developer currently focused on web technologies, interop between FOSS and Microsoft stacks, and deployment automation.

Updated on July 09, 2022

Comments

  • Yes - that Jake.
    Yes - that Jake. almost 2 years

    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?