c# - 如何处理 XML 中的非法字符

标签 c# xml winforms encoding

How should I handle this situation?

我将编码设置为 UTF8 但仍然出现错误...

alt text

我从 MemoryStream 对象创建该字符串(我将其设置为 WebBrowser.DocumentText),并像这样结束它:

Byte[] buffer = new Byte[ms.Length];
buffer = ms.ToArray();
return System.Text.Encoding.UTF8.GetString(buffer);

我错过了什么?

最佳答案

您将 XML 字符串作为文件名传递。

你可能想写

File.WriteAllText(saveFileDialog.FileName, wb.DocumentText);

或者,

using(StreamWriter writer = new StreamWriter(saveFileDialog.OpenFile(), false, Encoding.UTF8)) {
    write.Write(wb.DocumentText);
}

关于c# - 如何处理 XML 中的非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3522300/

相关文章:

c# - WCF从客户端发送int[]到服务器

php - 在 PHP 中轻松获取 XML 键和值

c# - 将线程安全的 UI 访问器保持在 c# 中的单独类中

c# - Visual Studio 2008 html 项目文档

C# 数据库连接延迟

.net - 有没有一种方法可以使用 c# 或 vb 从 xml 递归地找到最里面的节点

java - 如何将 JAXB 对象解码为 COM?

c# - 进度条值只是闪烁并消失

c# - 如何在不被迫以 Windows 窗体结束的情况下制作动画?

c# - 交叉发布时不显示目标 aspx 页面