Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获得&引用;在保存()后的XML文件开头_C#_.net_Xml_Winforms_.net 4.0 - Fatal编程技术网

C# 获得&引用;在保存()后的XML文件开头

C# 获得&引用;在保存()后的XML文件开头,c#,.net,xml,winforms,.net-4.0,C#,.net,Xml,Winforms,.net 4.0,我正在用C#打开一个现有的XML文件,并替换其中的一些节点。一切正常。保存后,我会在文件开头显示以下字符:  (EF BB BF in HEX) 整个第一行: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 我正在使用.NET4.0中的C#应用程序 有什么想法吗?为什么会这样?我们能以某种方式禁用它吗?它是用于AdobeInCopy的,它不会像这样打开它 更新: 替代解决方案: 使用XmlTextWr

我正在用C#打开一个现有的XML文件,并替换其中的一些节点。一切正常。保存后,我会在文件开头显示以下字符:

  (EF BB BF in HEX)
整个第一行:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
我正在使用.NET4.0中的C#应用程序

有什么想法吗?为什么会这样?我们能以某种方式禁用它吗?它是用于AdobeInCopy的,它不会像这样打开它

更新: 替代解决方案:

使用XmlTextWriter保存也可以:

XmlTextWriter writer = new XmlTextWriter(inCopyFilename, null);
doc.Save(writer);
这是一个UTF-8(BOM),是可以预期的。

这是一个标准,Unicode标准实际上不鼓励:

使用BOM既不是必需的,也不是必需的 建议用于UTF-8,但可能是 在UTF-8的上下文中遇到 数据从其他编码转换而来 使用BOM表或BOM表所在位置的表单 用作UTF-8签名

您可以使用以下方法禁用它:

var sw = new IO.StreamWriter(path, new System.Text.UTF8Encoding(false));
doc.Save(sw);
sw.Close();

您可以尝试更改XmlDocument的编码。下面是从中复制的示例

使用系统;使用System.IO;使用System.Xml;
公共类样本{
公共静态void Main(){
//创建并加载XML文档。
XmlDocument doc=新的XmlDocument();
string xmlString=“奥伯伦的遗产”;
doc.Load(新的StringReader(xmlString));
//创建一个XML声明。
xmldecl;
xmldecl=doc.CreateXmlDeclaration(“1.0”,null,null);
xmldecl.Encoding=“UTF-16”;
xmldecl.Standalone=“是”;
//将新节点添加到文档中。
XmlElement根=doc.DocumentElement;
doc.InsertBefore(xmldecl,root);
//显示修改后的XML文档
Console.WriteLine(doc.OuterXml);
} 

}

正如大家提到的,这是Unicode问题


我建议你试试。虽然不是很相关,但我提到它是因为它与旧方法相比非常简单,而且更重要的是,我认为它可能会自动解决这些问题,而无需您进行额外编码。

嗯,我从来不知道它会被劝阻。。。如果是,那么程序应该如何检测编码?@Lambert:XML在头中指定编码,或者(缺少该编码)默认为UTF-8。@Lambert:for UTF-8是短语的关键部分。如果你知道它是utf-8,那么就没有意义,没有终端问题。在没有bom的情况下读取utf-16be编码的xml文件的几率仍然很小,即使在处理指令中声明了它。感谢所有的答案。这很有帮助。我已经用在您输入后找到的另一个解决方案更新了这个问题。此代码不可编译。
StreamWriter
的第一个参数是
流,而不是路径。另外,
sw
如果
doc.Save(sw)将永远不会关闭引发异常。
使用
语句的经典案例。请参见此处-Jon Skeet解释了如何在保存XMLDocument时使用remove the BOM,如果您需要的话。
var sw = new IO.StreamWriter(path, new System.Text.UTF8Encoding(false));
doc.Save(sw);
sw.Close();
using System; using System.IO; using System.Xml;

public class Sample {

  public static void Main() {

    // Create and load the XML document.
    XmlDocument doc = new XmlDocument();
    string xmlString = "<book><title>Oberon's Legacy</title></book>";
    doc.Load(new StringReader(xmlString));

    // Create an XML declaration. 
    XmlDeclaration xmldecl;
    xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
    xmldecl.Encoding="UTF-16";
    xmldecl.Standalone="yes";     

    // Add the new node to the document.
    XmlElement root = doc.DocumentElement;
    doc.InsertBefore(xmldecl, root);

    // Display the modified XML document 
    Console.WriteLine(doc.OuterXml);

  }