【问题标题】:How to load xml file to xamarin android如何将xml文件加载到xamarin android
【发布时间】:2016-04-17 11:37:06
【问题描述】:

我将 Xml 文件保存在 Assets 文件夹中并将 Build Action 设置为 Android Assets,当我尝试将更改保存到 xml 文件时,它会给出异常 System.UnauthorizedAccessException: Access to the path "/Q317664.xml" is denied.i不知道为什么会发生这个异常,请帮助我

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

【问题讨论】:

    标签: c# android xml xamarin xamarin.android


    【解决方案1】:

    应用程序包中的Assets 目录是只读的,您无法写入您打开的Q317664.xml xml 文档。这就是System.UnauthorizedAccessException 的原因。

    如果您想修改捆绑在资产目录中的内容,请将其解压缩到可写位置,然后对该副本进行操作:

    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");
    

    见:

    【讨论】:

      【解决方案2】:

      您需要将 XML 文件添加到 Xamarin Android 项目的 Assets 文件夹中

      然后你可以像这样将 Xml 加载到变量中

      var xml = XDocument.Load(Assets.Open("data.xml"));
      

      【讨论】:

      • 我将 xml 文件放在 Assets 文件夹中,现在发生此错误 错误 1 ​​资源目录名称无效:“res assets”。 c:\users\asad\documents\visual studio 2013\Projects\App17\App17\aapt.exe App17
      • xml 文件的名称是什么?
      • 我现在通过将构建操作设置为 android Assets 解决了这个问题,当我在更改属性值后尝试保存 XML 文件时,它给出了异常 System.UnauthorizedAccessException: Access to the path "/Q317664.xml " 被拒绝。我不确定为什么会发生此异常,请帮助我
      • 请编辑问题并添加完整的源代码。您设置了哪个构建操作?
      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多