From 7b24c6d906016b7f497f06002cc24441dc65fcbc Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Thu, 19 Jan 2012 19:39:08 +0900 Subject: [PATCH] Fix bug #2383 patch by Weeble - in XPathExtensions.Evaluate(), return IEnumerable of the underlying object, not XPathNavigator, for node-set values. --- mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs | 14 +++++++++++++- .../Test/System.Xml.XPath/ExtensionsTest.cs | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs b/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs index cc3f448733a..7090cea9af7 100644 --- a/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs +++ b/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs @@ -27,6 +27,7 @@ #if !MOONLIGHT using System; +using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; @@ -52,7 +53,18 @@ namespace System.Xml.XPath public static object XPathEvaluate (this XNode node, string expression, IXmlNamespaceResolver nsResolver) { - return CreateNavigator (node).Evaluate (expression, nsResolver); + object navigationResult = CreateNavigator (node).Evaluate (expression, nsResolver); + if (!(navigationResult is XPathNodeIterator)) + return navigationResult; + return GetUnderlyingXObjects((XPathNodeIterator) navigationResult); + } + + private static IEnumerable GetUnderlyingXObjects(XPathNodeIterator nodeIterator) + { + foreach (XPathNavigator nav in nodeIterator) + { + yield return (XObject)(nav.UnderlyingObject); + } } public static XElement XPathSelectElement (this XNode node, string xpath) diff --git a/mcs/class/System.Xml.Linq/Test/System.Xml.XPath/ExtensionsTest.cs b/mcs/class/System.Xml.Linq/Test/System.Xml.XPath/ExtensionsTest.cs index 84ca719ce52..93d64290bed 100644 --- a/mcs/class/System.Xml.Linq/Test/System.Xml.XPath/ExtensionsTest.cs +++ b/mcs/class/System.Xml.Linq/Test/System.Xml.XPath/ExtensionsTest.cs @@ -30,6 +30,7 @@ // using System; +using System.Collections; using System.IO; using System.Linq; using System.Xml; @@ -452,5 +453,14 @@ namespace MonoTests.System.Xml nav.MoveToNext (); Assert.AreEqual ("", nav.GetNamespace (""), "#2." + nav.GetType ()); } + + [Test] // bug #2383 + public void EvaluateNodeSetAsEnumerable () + { + String xml = ""; + XDocument d = XDocument.Parse (xml); + IEnumerable att = (IEnumerable) d.XPathEvaluate ("/root/@a"); + att.Cast ().FirstOrDefault (); + } } } \ No newline at end of file -- 2.11.4.GIT