获得[我»¿"在我的XML文件保存后开始()我的、文件、XML、QUOT

2023-09-04 00:13:36 作者:冷血范

我打开用C#现有的XML文件,并替换我在那里的一些节点。一切工作正常。就在我保存它,我得到以下字符的文件的开头:

 (EF BB BF十六进制)
 

在整个第一行:

 <?XML版本=1.0编码=UTF-8独立=是>
 

该文件的其余部分看起来像一个正常的XML文件。 简化的code是在这里:

  XmlDocument的文档=新的XmlDocument();
doc.Load(xmlSourceFile);
XmlNode的翻译= doc.SelectSingleNode(//反式单元[@ ID ='127']);
translation.InnerText =测试;
doc.Save(xmlTranslatedFile);
 
苹果账号集体被盗刷 赶紧看看你有没有中招

我使用的是C# Windows窗体应用程序与.NET 4.0。

任何想法?为什么会这样?我们可以禁用不知何故?这是对Adobe InCopy中,它不会打开它这个样子。

更新: 另一种解决方案:

与XmlTextWriter的保存它也能工作:

  XmlTextWriter的作家=新的XmlTextWriter(inCopyFilename,NULL);
doc.Save(作家);
 

解决方案

这是 UTF-8 BOM ,它实际上是由统一code标准气馁:

  

http://www.uni$c$c.org/versions/Uni$c$c5.0.0/ch02.pdf

     

既不需要使用一个BOM,也不能   建议对于UTF-8,但也可以是   在环境中遇到的地方UTF-8   数据从其他的编码转换   使用一个BOM或在BOM表   作为UTF-8签名

使用您可以禁用它:

 变种SW =新IO.StreamWriter(道路,新System.Text.UTF8Encoding(假));
doc.Save(SW);
sw.Close();
 

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:

  (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:

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.

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.

UPDATE: Alternative Solution:

Saving it with the XmlTextWriter works too:

XmlTextWriter writer = new XmlTextWriter(inCopyFilename, null);
doc.Save(writer);

解决方案

It is the UTF-8 BOM, which is actually discouraged by the Unicode standard:

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

You may disable it using:

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

 
精彩推荐
图片推荐