【问题标题】:Conversion buffer overflow when saving updated XML to file将更新的 XML 保存到文件时转换缓冲区溢出
【发布时间】:2016-04-21 00:05:40
【问题描述】:
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>
<Collection>
<Book Id='1' ISBN='1-100000ABC-200'>
<Title>Principle of Relativity</Title> 
</Book>
</Collection>

每当我想将更新的 XML 文件保存在应用程序目录 /data/data/App17.App17/files 中时,就会发生异常

System.ArgumentException:转换缓冲区溢出。

为什么会出现这个异常,我该如何解决?

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


        SetContentView(Resource.Layout.Main);
        var xml = XDocument.Load(Assets.Open("Q317664.xml"));
        var node = xml.Descendants("Book").FirstOrDefault(cd => cd.Attribute("Id").Value == "1");
        node.SetAttributeValue("ISBN", "new");

        string dir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string path = Path.Combine(dir, "Q317664.xml");

       xml.Save(path);

【问题讨论】:

  • 请向我们提供 StackTrace

标签: android file xamarin xamarin.android filepath


【解决方案1】:

问题是,文件没有以 UTF-8 编码。我假设,你已经用Visual Studio &gt; New File 创建了它。这会创建一个带有 Windows 代码页或其他编码的文件。

在VS中打开文件并通过File &gt; Advanced Save Options保存,然后选择Unicode - Codepage 1200

完成此操作后,您的代码应该可以正常运行了。

【讨论】:

  • Stube 现在我想在我的 Asset 文件夹中添加这个更新的 Xml 文件,当应用程序正在运行但保留旧文件时,请帮助我。
  • 你已经问过这个问题了:stackoverflow.com/questions/36677276/…
  • Stube 现在我想将这个保存 XML 文件从 app 目录添加到 Asset 文件夹,但保留旧的 XML 文件,它已经存在于 Asset 文件夹中
  • 他们已经(多次)告诉您资产文件夹是只读的。
  • Stube 你没有得到我的问题我是说我更新了 XML 文件并将该文件保存在 App 目录中 OK 现在我想从 App 目录中将更新后的文件包含在我的 Asset 文件夹中,你知道了与否。
猜你喜欢
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 2015-12-16
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
相关资源
最近更新 更多