无法将更改保存在XML文件中 [英] Unable to save the changes in XML file
本文介绍了无法将更改保存在XML文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试保存XML文件时,我遇到了问题,我将XML文件存储在Assets文件夹中,并将构建操作设置为Android Assets,但是当xml.Save("Q317664.xml")时; 出现异常行System.UnauthorizedAccessException:拒绝访问路径"/Q317664.xml".我不确定为什么会发生这种情况
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++); };
}
}
}
推荐答案
在Xamarin Android中
In Xamarin Android
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filename = Path.Combine(path, "myFile.xml");
File.WriteAllText(filename,xml.ToString());
这篇关于无法将更改保存在XML文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文