0

i save the Xml file in Assets folder and set Build Action to Android Assets now when i try to save the changes to xml file it gives exception System.UnauthorizedAccessException: Access to the path "/Q317664.xml" is denied.i am not sure why this exception is occurring please help me

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;
using System.Reflection;
using Android.Content.Res;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App17 
{
[Activity(Label = "App17", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    int count = 1;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        var xml = XDocument.Load(Assets.Open("Q317664.xml"));
        var node = xml.Descendants("Book").FirstOrDefault(cd => cd.Attribute("Id").Value == "1");
        node.SetAttributeValue("ISBN", "new");
        xml.Save("Q317664.xml");
     //   Button button = FindViewById<Button>(Resource.Id.MyButton);

       // button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
    }
}
}
Asad Ahmad
  • 29
  • 1
  • 9

2 Answers2

1

The Assets directory in the application bundle is read-only, you cannot write to the Q317664.xml xml document that you open. This is the cause of the System.UnauthorizedAccessException.

If you want to modify something bundled in the assets directory, extract it to a writeable location and then operate on that copy:

string fileName = "Q317664.xml";
string filePath = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.ToString (), fileName);
// Check if your xml file has already been extracted.
if (!File.Exists(filePath))
{
    using (BinaryReader br = new BinaryReader(Assets.Open(fileName)))
    {
        using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
        {
            byte[] buffer = new byte[2048];
            int len = 0;
            while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
            {
                bw.Write (buffer, 0, len);
            }
        }
    }
}

// Operate on the external file
var xml = XDocument.Load(filePath);
var node = xml.Descendants("Book").FirstOrDefault(cd => cd.Attribute("Id").Value == "1");
node.SetAttributeValue("ISBN", "new");
xml.Save("Q317664.xml");

See:

Community
  • 1
  • 1
matthewrdev
  • 11,930
  • 5
  • 52
  • 64
0

You need to Add the XML file to Assets folder in Xamarin Android project

Then you can load Xml into variable like this

var xml = XDocument.Load(Assets.Open("data.xml"));
Sreeraj
  • 2,306
  • 2
  • 18
  • 31
  • I place the xml file in Assets folder now this error is occurring Error 1 Invalid resource directory name: "res assets". c:\users\asad\documents\visual studio 2013\Projects\App17\App17\aapt.exe App17 – Asad Ahmad Apr 17 '16 at 12:19
  • Whats the name of the xml file? – Sreeraj Apr 17 '16 at 12:53
  • i have solved the problem by setting build action to android Assets now when i try to save the XML file after changing the attribute value it gives exception System.UnauthorizedAccessException: Access to the path "/Q317664.xml" is denied.i am not not sure why this exception is occurring please help me – Asad Ahmad Apr 17 '16 at 13:23
  • Please edit the question and add full source code.And which build action have you set ? – Sreeraj Apr 17 '16 at 13:26