如何将XML文件元素保存到xamarin android中的ExternalStorageDirectory?

时间:2018-03-12 10:20:24

标签: c# xml xamarin

我尝试使用下面的代码但是收到System.UnauthorizedAccessException的错误拒绝访问该路径?

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

        // Create your application here
        SetContentView(Resource.Layout.CreateEvent);

        EditText EventName = (EditText)FindViewById(Resource.Id.eventname);
        EditText EvenDescription = (EditText)FindViewById(Resource.Id.eventdesciprtion);
        string date = DateTime.Now.ToString();
        Button Submitevent = (Button)FindViewById(Resource.Id.submitevent);

        Submitevent.Click += (s, e) =>
        {

            //string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            //string filename = Path.Combine(path, "storedata.xml");

            var sdcardpath = Android.OS.Environment.ExternalStorageDirectory.Path;
            string filename = Path.Combine(sdcardpath, "storedata.xml");
            string saving = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

            File.SetAttributes(filename, FileAttributes.Normal);

            XDocument xdoc = new XDocument();
            if (!File.Exists(filename))
            {
                File.Create(filename);
                xdoc = XDocument.Load(filename);
                xdoc.Add(new XElement("Events"));
            }

            xdoc = XDocument.Load(Assets.Open("storedata.xml"));
            XElement _event = new XElement("Event");

            _event.Add(new XAttribute("Name", EventName.Text),
                       new XAttribute("Description", EvenDescription.Text),
                       new XAttribute("Date", date));

            xdoc.Root.Add(_event);
            xdoc.Save(saving);//getting error at this line System.UnauthorizedAccessException 


        };

错误在我将数据保存到扩展区上的xml时显示在图像中

Error Is shown in Image while i am saving the data to xml on exte

0 个答案:

没有答案