user2962761
user2962761

Reputation: 130

Load embedded xml in xamarin (c#)

I'm new to android/xamarin. I have an embedded xml file that I want to read in "Resources\xml\settings.xml" but I can't open it with the XmlDocument();

Can anyone help me correct this code?

XmlDocument doc = new XmlDocument ();
doc.Load ("Resources\xml\settings.xml");
int c = 0;
foreach (XmlNode item in doc.ChildNodes) {
    name[c] = item.ChildNodes [0].InnerText;
    pref[c] =  item.ChildNodes [1].InnerText;
    c++;
}

It always gives me an error in the "doc.load(..." line and I've tried everything. (Build action is set to embeddedResource)

Thanks in advance.

Upvotes: 1

Views: 4699

Answers (1)

Jason
Jason

Reputation: 89082

Instead of using a Resoure, add the file as an Asset.

string content;
using (StreamReader sr = new StreamReader (Assets.Open ("settings.xml")))
{
    content = sr.ReadToEnd ();
}

XmlDocument doc = new XmlDocument ();
doc.LoadXml (content);

Upvotes: 4

Related Questions