views:

1861

answers:

4

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?

A: 

What is the use of variable pattern?
Is it a reference to the DOM of the entire XML?

See what this results into pattern.SelectNodes("//dictionary/").ChildNodes.Count

EDIT: Is this well-formed xml?

shahkalpesh
pattern is an XMLNode that contains the XML shown above.
Jekke
A: 

Could namespaces be causing a problem? Also, try looking at "pattern.OuterXml" to make sure you are looking at the correct element.

David
There are no namespaces defined. And, I've checked the outer XML to make sure that I'm loading what I think I'm loading.
Jekke
Then what about just looping through pattern.ChildNodes[0].ChildNodes ?
David
+2  A: 

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?

d1k_is
+1  A: 

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

marc_s