将更新的 XML 保存到文件时转换缓冲区溢出

Conversion buffer overflow when saving updated XML to file

<?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: Conversion buffer overflow.

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

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

问题是文件未以 UTF-8 编码。我假设您是使用 Visual Studio > New File 创建的。这将创建一个具有 Windows 代码页或其他编码的文件。

在 VS 中打开文件并通过 File > Advanced Save Options 保存,然后 select Unicode - Codepage 1200.

完成此操作后,您的代码应该 运行 没问题。