-2

I will be glad to any help. How correctly to specify a way to an xml a file?

My application is crashing out of this code.

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

screen - Here is my xml file

Sorry gyus. I will try to correct myself.

When testing the application through XamarinLive no problems.

After the application is archived and signed. I install the application (file .apk) on the phone. I launch the application on the phone. I turn to pages with a problem code and the application crashes.

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("XMLBase/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
  • 2
    By "crashing" you surely mean you get an exception. So it would be very nice to tell us the exact exception message. – René Vogt May 07 '18 at 15:37
  • Take a look at [How can I discover the “path” of an embedded resource?](https://stackoverflow.com/q/27757), [Reading embedded XML file c#](https://stackoverflow.com/q/2820384) and/or [Load an XML from Resources](https://stackoverflow.com/q/851813). – dbc May 07 '18 at 15:41
  • Is there a reason that you think to start the path at your XMLBase folder? What if there was an XMLBase folder elsewhere in your folder structure? How would your program know which to use? – Broots Waymb May 07 '18 at 15:41

1 Answers1

1

This code fragment below can be used as a debug/learning aid to see all the manifest resource names in your assembly. Then, use the names shown as needed in your assembly for your resources.

var assembly = Assembly.GetExecutingAssembly();

foreach (var resourceName in assembly.GetManifestResourceNames())
    System.Console.WriteLine(resourceName);

Screen shot #1 shows an example of the resource names produced by the code fragment above in my particular assembly. The one in red is used in the code fragment below. The code fragment below is an example of how I read the resource into a string variable (a .txt file in my case)

Screen shot #2 shows the resources's properties. The resource needs to be an Embedded Resource for the code to work

string excludedTablesString = new StreamReader((Assembly.GetExecutingAssembly()).GetManifestResourceStream("DataReviewUtility.Resources.ExcludedTables.txt")).ReadToEnd();

Screen Shot #1

enter image description here

Screen Shot #2

enter image description here

VA systems engineer
  • 2,856
  • 2
  • 14
  • 38