XML : C# XPathSelectElement() when there are multiple matches

We're getting unexpected results in an application I support, because the code is calling XPathSelectElement() on a path that matches multiple items. To illustrate:

  XElement e = XElement.Parse(@"<root>                                  <child ID="123">A</child>                                  <child ID="234">B</child>                                </root>");  Console.Out.WriteLine(e.XPathSelectElement("//child").Value);    

In this simple case, it's returning "A". In our more complex production code, it seems to be returning the last match.

To be clear, I'm going to fix this bug with extra filtering in the XPath to make the query result unique. I don't want to depend on misusing the XPathSelectElement() method.

However, I'd like to be sure I understand what it's currently (incorrectly) doing. I haven't been able to find any documentation on what the expected behavior is in this case. Or is this just a non-standard behavior that you can't expect to behave consistently across environments/versions/etc?

No comments:

Post a Comment