Bug 2383 - System.Xml.XPath.Extensions.XPathEvaluate returns wrong result types
Summary: System.Xml.XPath.Extensions.XPathEvaluate returns wrong result types
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: master
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-12-07 07:45 UTC by Weeble
Modified: 2012-01-19 05:42 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Possible fix (1.18 KB, patch)
2011-12-07 08:54 UTC, Weeble
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Weeble 2011-12-07 07:45:24 UTC
Description of Problem:

XPathEvaluate's documentation says that it returns:

Return Value
Type: System.Object
An object that can contain a bool, a double, a string, or an IEnumerable<T>. 
...
If the collection is an enumeration of elements or attributes, you can use the Cast operator to get a collection of XElement or XAttribute.

However, running in Mono, the returned object, when enumerated, does not produce instances of XElement or XAttribute (of course assuming that the XPath expression identified elements or attributes). Instead it returns instances of XNodeNavigator.

Steps to reproduce the problem:

1. Compile and run the sample code from the XPathEvaluate documentation:

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.XPath;

public class Program
{
    public static void Main()
    {
        String xml = "<root a='value'/>";
        XDocument d = XDocument.Parse(xml);
        IEnumerable att = (IEnumerable)d.XPathEvaluate("/root/@a");
        Console.WriteLine(att.Cast<XAttribute>().FirstOrDefault());
    }
}


Actual Results:

<No output>

Expected Results:

a="value"

How often does this happen? 

100%

Additional Information:

See the XPathEvaluate docs: http://msdn.microsoft.com/en-us/library/bb341675(v=VS.90).aspx

This works as expected on MS .NET using Visual Studio 2010.

Tested on both mono2.6.7-5ubuntu3 and trunk (revision 880bea0e62f7b4d97eab34fde65c5121e706ea72).
Comment 1 Weeble 2011-12-07 08:54:41 UTC
Created attachment 1010 [details]
Possible fix

I'm not 100% sure about this patch, but it gives me the expected behaviour for the cases that affect me. I don't have a Windows box handy to find out how closely it matches - e.g. when MS.NET's XPathEvaluate returns an IEnumerable<T>, I'm not sure if it could be an IEnumerable<XObject>, an IEnumerable<XElement>, an IEnumerable<XAttribute> or even an IEnumerable<object>.
Comment 2 Weeble 2011-12-08 05:51:08 UTC
I compiled and ran this test code on MS.NET and Mono:

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.XPath;

public class Program
{
    public static void Main()
    {
        String xml = "<root a='value'/>";
        XDocument d = XDocument.Parse(xml);
        IEnumerable att = (IEnumerable)d.XPathEvaluate("/root/@a");
        Console.WriteLine("Type: {0}", att.GetType());
        Console.WriteLine("Interfaces:");
        foreach (Type interfaceType in att.GetType().GetInterfaces())
        {
            Console.WriteLine("  {0}", interfaceType);
        }
    }
}

Results:

MS.NET
======

Type: System.Xml.XPath.XPathEvaluator+<EvaluateIterator>d__0`1[System.Object]
Interfaces:
  System.Collections.Generic.IEnumerable`1[System.Object]
  System.Collections.IEnumerable
  System.Collections.Generic.IEnumerator`1[System.Object]
  System.IDisposable
  System.Collections.IEnumerator

Mono
====
Type: System.Xml.XPath.SimpleSlashIterator
Interfaces:
  System.Collections.IEnumerable
  System.ICloneable

So it looks like .NET provides an IEnumerable<object>. My patch provides an IEnumerable<XObject>, so it might be better to change it to IEnumerable<object>.
Comment 3 Atsushi Eno 2012-01-19 05:42:14 UTC
Sorry for belated reply - it is a good catch and your change looks nice. I have applied it in our git master 7b24c6d. Thanks!