Masciuniria
Masciuniria

Reputation: 172

Can't load XML from Resource/Drawable/

I've saved my Xml in Resource/Drawable/ . I put it here cause is the only place where it can stay without any error.

Any way this is the exception:

System.IO.FileNotFoundException: Could not find file "/sds".

How can I find the path?

Here is my code:

String conditionName = "";
XDocument xml = XDocument.Load("@drawable/LightConditions.xml");                
foreach (XElement item in xml.Root.Elements("Condizione"))
{
    if (item.Element("EV").Value == EV)
    {
        conditionName = item.Parent.Element("Nome").Value;
        break;
    }
    //do something
}

Upvotes: 0

Views: 767

Answers (1)

SushiHangover
SushiHangover

Reputation: 74094

You will want to place that .xml in the Assets folder and flag the build action as an AndroidAsset.

Then you can use the AssetManager to access it via a Stream:

string conditionName = "";
using (StreamReader sr = new StreamReader(this.Assets.Open("LightConditions.xml")))
{
    XDocument xml = XDocument.Load(sr);
    foreach (XElement item in xml.Root.Elements("Condizione"))
    {
        if (condizione.Element("EV").Value == EV)
        {
            conditionName = item.Parent.Element("Nome").Value;
            break;
        }
        //do something
    }
}

Consult the Xamarin.Android guide on Assets

Upvotes: 2

Related Questions