0

How to load an XML file from a file in the solution, build with Embedded Resource? Cross Platform (Xamarin.Forms) - VS

XDocument xDoc = XDocument.Load("Data.xml");

Here's an outline of my solution: Here's an outline of my solution.IMAGE

Full page code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using System.Xml.Linq;
using Xamarin.Forms.Xaml;

namespace App14
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ListMain : ContentPage
    {

        public List<string> Students; 

        public ListMain ()
        {

            InitializeComponent ();
            Students = new List<string>(); 
            XDocument xDoc = XDocument.Load("Data.xml");
            foreach (XElement xe in xDoc.Element("students").Elements("mainStudent"))
            {
                Students.Add(xe.Element("student").Value); 
            }

            foreach (string student in Students)
            {
               stackLayout.Children.Add(new Label { Text = student, FontSize = 20, HorizontalOptions = LayoutOptions.StartAndExpand });
               stackLayout.Children.Add(new Label { Text = "DEL", FontSize = 20, HorizontalOptions = LayoutOptions.End });
            }


        }


    }
}
Alexander
  • 17
  • 4
  • See https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Bursac Milan May 11 '18 at 12:03
  • What's wrong with this code? What issue you are facing with this code? – Chetan May 11 '18 at 12:38
  • Does the solution for .Net on Windows not work on Xamarin? See [Reading embedded XML file c#](https://stackoverflow.com/q/2820384) and [How to read embedded resource text file](https://stackoverflow.com/q/3314140). – dbc May 11 '18 at 14:48
  • See also: https://stackoverflow.com/a/7720957/353147 – Chuck Savage May 12 '18 at 07:36

1 Answers1

0

If you want a cross embedded resource, you should place a file inside a shared or PCL project.

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=vswin#embedded-images

Suyon Won
  • 346
  • 2
  • 8