Unable to use System.Xml or System.Xml.Linq in crossplatform project in VS.

StyrmirThorarinssonStyrmirThorarinsson ISMember

I've created a Xamarin.Forms project and am working on an Android implementation.

So the main project [Projectname] contains folders such as Helpers, Models, Services, Viewmodels but no reference section.

And [Projectname].Android contains a reference section and all needed android files.

The project I'm working on should read and parse XML files online.

I've managed to make it work in a .NET test module as an independent function.
However whenever I try to test similar code placed in the core business project code part of the application (not in [projectname].Android but the [Projectname] part that is.). The creation of any sort of objects that inherit from XmlReader fail with an error such as follows:

"Message: Test method UnitTestProjectNET.UnitTest1.TestGetSubDivisions threw exception:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified."

or

"Message: Test method UnitTestProjectNET.UnitTest1.TestLoadSubdivisionXMLData threw exception:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Xml.Linq, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified."

The project [Projectname].Android references both System.Xml and System.Xml.Linq.
However there is no reference section for the main project [Projectname], which is where this code runs from and no apparent way to add references to it. After trying a number of different ways to code this and double checking that both references are in fact in place for the [Projectname].android project, plus making sure I reference what code library I'm using in the code these errors still persist.

Here are two examples of XML reading code methods that both create those errors above in the project, but both work if I run them in the .NET test project:

    public void getSubDivisions()
    {    
        string uri = "http://api.vateud.net/subdivisions.xml";
        XmlTextReader r = new XmlTextReader(uri); 

   //...
 }



    public async void loadSubdivisionXMLData()
    {
        List<VatsimSubdivision> rawData = null;
        await Task.Factory.StartNew(delegate {
            XDocument doc = XDocument.Load("http://api.vateud.net/subdivisions.xml");
            IEnumerable<VatsimSubdivision> divisions = from d in doc.Descendants("object")
                                      select new VatsimSubdivision
                                      {
                                          code = d.Attribute("code").Value,
                                          introText = d.Attribute("introtext").Value,
                                          name = d.Attribute("name").Value,
                                          official = Convert.ToBoolean(d.Attribute("official").Value),
                                          website = d.Attribute("website").Value
                                      };
            rawData = divisions.ToList();
        });

        Console.WriteLine("XML reading complete");
    }

Answers

  • I've managed to zero in on the issue.
    The code snippets above will both run if run in an independent project, be it a test project or android specific project.

    The missing assembly error occurs when I try to test a method that require the assemblies needed above from the test project calling a method in the android project.

    This is rather odd as both the test project and the android projects have references to the same versions of System.Xml and System.Xml.Linq. And the test project has a reference to the android project.

    Does anyone know why this might be happening?

  • manpakhongmanpakhong HKMember

    I confronted the same problem as you. I created an xamarin android project. I created an unit test project depend on the xamarin android project. I use NLog and when I read xml configuration. It throws exception:

    Could not load file or assembly 'System.Xml, Version=2.0.5.0 xamarin ......

  • HospifyHospify GBMember ✭✭

    It does seem to be something about referencing a shared project from NUnitTests. I get the same exception for linking both to the Android and iOS shared projects. Have either of you posted a bug report on this?

  • hey guys does anyone find the solution, I'm facing the same problem for like hours

Sign In or Register to comment.