This question already has an answer here:
这个问题已经有了答案:
- How can I remove the BOM from XmlTextWriter using C#? 2 answers
- 如何使用c#从XmlTextWriter删除BOM ?2答案
I'm opening an existing XML file with C#, and I replace some nodes in there. All works fine. Just after I save it, I get the following characters at the beginning of the file:
我用c#打开一个现有的XML文件,并替换其中的一些节点。所有工作正常。在保存之后,我在文件的开头找到以下字符:
 (EF BB BF in HEX)
The whole first line:
整个第一行:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
The rest of the file looks like a normal XML file. The simplified code is here:
文件的其余部分看起来像一个普通的XML文件。简化代码如下:
XmlDocument doc = new XmlDocument();
doc.Load(xmlSourceFile);
XmlNode translation = doc.SelectSingleNode("//trans-unit[@id='127']");
translation.InnerText = "testing";
doc.Save(xmlTranslatedFile);
I'm using a C# Windows Forms application with .NET 4.0.
我正在使用c# Windows窗体应用程序和。net 4.0。
Any ideas? Why would it do that? Can we disable that somehow? It's for Adobe InCopy, and it does not open it like this.
什么好主意吗?为什么会这样?我们能以某种方式禁用它吗?它是用于Adobe InCopy的,它不会像这样打开它。
UPDATE: Alternative Solution:
更新:可选择的解决方案:
Saving it with the XmlTextWriter works too:
使用XmlTextWriter保存它也可以:
XmlTextWriter writer = new XmlTextWriter(inCopyFilename, null);
doc.Save(writer);
4 个解决方案
#1
38
It is the UTF-8 BOM, which is actually discouraged by the Unicode standard:
它是UTF-8 BOM,它实际上被Unicode标准阻止了:
http://www.unicode.org/versions/Unicode5.0.0/ch02.pdf
http://www.unicode.org/versions/Unicode5.0.0/ch02.pdf
Use of a BOM is neither required nor recommended for UTF-8, but may be encountered in contexts where UTF-8 data is converted from other encoding forms that use a BOM or where the BOM is used as a UTF-8 signature
UTF-8既不需要也不推荐使用BOM,但在使用BOM的其他编码形式转换UTF-8数据或使用BOM作为UTF-8签名的环境中可能会遇到这种情况
You may disable it using:
您可以使用以下方式禁用它:
var sw = new IO.StreamWriter(path, new System.Text.UTF8Encoding(false));
doc.Save(sw);
sw.Close();
#3
0
You can try to change the encoding of the XmlDocument. Below is the example copied from MSDN
您可以尝试更改XmlDocument的编码。下面是从MSDN复制的示例
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);
}
}
}
#4
0
As everybody else mentioned, it's Unicode issue.
正如大家提到的,这是Unicode问题。
I advise you to try LINQ To XML. Although not really related, I mention it as it's super easy compared to old ways and, more importantly, I assume it might have automatic resolutions to issues like these without extra coding from you.
我建议您尝试使用LINQ到XML。虽然不是很相关,但我提到它是因为它比旧的方法更容易,而且更重要的是,我认为它可能会自动解决类似问题,而不需要您进行额外的编码。
#1
38
It is the UTF-8 BOM, which is actually discouraged by the Unicode standard:
它是UTF-8 BOM,它实际上被Unicode标准阻止了:
http://www.unicode.org/versions/Unicode5.0.0/ch02.pdf
http://www.unicode.org/versions/Unicode5.0.0/ch02.pdf
Use of a BOM is neither required nor recommended for UTF-8, but may be encountered in contexts where UTF-8 data is converted from other encoding forms that use a BOM or where the BOM is used as a UTF-8 signature
UTF-8既不需要也不推荐使用BOM,但在使用BOM的其他编码形式转换UTF-8数据或使用BOM作为UTF-8签名的环境中可能会遇到这种情况
You may disable it using:
您可以使用以下方式禁用它:
var sw = new IO.StreamWriter(path, new System.Text.UTF8Encoding(false));
doc.Save(sw);
sw.Close();
#2
#3
0
You can try to change the encoding of the XmlDocument. Below is the example copied from MSDN
您可以尝试更改XmlDocument的编码。下面是从MSDN复制的示例
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);
}
}
}
#4
0
As everybody else mentioned, it's Unicode issue.
正如大家提到的,这是Unicode问题。
I advise you to try LINQ To XML. Although not really related, I mention it as it's super easy compared to old ways and, more importantly, I assume it might have automatic resolutions to issues like these without extra coding from you.
我建议您尝试使用LINQ到XML。虽然不是很相关,但我提到它是因为它比旧的方法更容易,而且更重要的是,我认为它可能会自动解决类似问题,而不需要您进行额外的编码。