Unable to save the changes in XML file

 ̄綄美尐妖づ 提交于 2019-12-25 06:58:20

问题


i am facing problem when i try to save the XML file i store the XML file in Assets folder and set build action to Android Assets but when xml.Save("Q317664.xml"); line comes it gives exception System.UnauthorizedAccessException: Access to the path "/Q317664.xml" is denied.i am not sure why this is occurring

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++); };
    }
}
}

回答1:


We cannot edit and save the XML file in Assets folder. Files in Assets folder are files containing static data that are shipped with APK. We cannot write to it. You can read the xml edit it and then probably save the file to App's Directory.




回答2:


In Xamarin Android

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filename = Path.Combine(path, "myFile.xml");

File.WriteAllText(filename,xml.ToString());


来源:https://stackoverflow.com/questions/36677276/unable-to-save-the-changes-in-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!