Search code examples
linqxamarinxelement

Querying XElement in a Xamarin Android App


The method I have been using to query an XElement works in Windows but not in Xamarin Android:

using System.Xml;
using System.Xml.Linq;
using System.Linq;

    public string ValidateLicense()
    {
        XElement xEl;
        string sMessage;
        Model.clsWebServiceFunctions cSvc = new Model.clsWebServiceFunctions();

        sMessage = cSvc.QueryWEB_ValidateLicense();
        xEl = cSvc.ServiceGetData(clsSettings.LicensingUrl, sMessage);

        var vResult = from results in xEl.DescendantsAndSelf("Result")
                      select results;

        return vResult.First().Element("LicensingResult").Value;
    }

The message I get is

Method 'First' not found in type'IEnumerable'1'

This seems to be due to Xamarin not supporting this method?!

I have do lots of similar queries in my app, so can someone recommend an alternative that will work in Xamarin? I could bodge this but don't know a good solution.


Solution

  • I'm having a similar issue on iOS.

    This seems to be a problem with the Linq extension methods to IEnumerable not being loaded.

    The error occurs within MvvmCross which is designed to work with the mobile platforms Xamarin supports. The issue doesn't occur initially when a fresh app is deployed, it takes some time to manifest, ie do a few days dev, attempt to run again on simulator.

    Therefore I believe this is a linking problem with Xamarin. [Edit]: Not anymore, see below.

    http://forums.xamarin.com/discussion/19483/pcl-linq-problem-on-ios-vs2013-sp2 - No answers here yet.

    [Edit] OK So I was referencing a NET FX 3.5 DLL. changing the referenced assembly to NET 4.0 seemed to resolve the problem. Note that in the app output you can see this on startup. This is bad:

    Loaded assembly: /Users/mike/Library/Caches/Xamarin/mtbs/builds/XamTestTouch2/7da682ee-1ab5-4b07-b745-c3c558a10329/assemblies/System.Core.dll [External]
    

    This is good:

    Loaded assembly: /Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll [External]
    

    You may need to delete the Caches/Xamarin/mtbs/builds/AppName/GUID00-00000-0000.. folder after fixing your references to get things working again.