.net XML - 根级别的数据无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/291455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML - Data At Root Level is Invalid
提问by George Stocker
I have an XSD file that is encoded in UTF-8, and any text editor I run it through doesn't show any character at the beginning of the file, but when I pull it up in Visual Studio's debugger, I clearly see an empty box in front of the file.
我有一个以 UTF-8 编码的 XSD 文件,我运行它的任何文本编辑器都没有在文件的开头显示任何字符,但是当我在 Visual Studio 的调试器中拉出它时,我清楚地看到一个空的文件前面的框。


I also get the error:
我也收到错误:
Data at the root level is invalid. Line 1, position 1.
根级别的数据无效。第 1 行,位置 1。


Anyone know what this is?
有谁知道这是什么吗?
Update: Edited post to qualify type of file. It's an XSD file created by Microsoft's XSD creator.
更新:编辑帖子以限定文件类型。它是由 Microsoft 的 XSD 创建者创建的 XSD 文件。
回答by George Stocker
It turns out, the answer is that what I'm seeing is a Byte Order Mark, which is a character that tells whatever is loading the document what it is encoded in. In my case, it's encoded in utf-8, so the corresponding BOM was EF BB BF, as shown below. To remove it, I opened it up in Notepad++and clicked on "Encode in UTF-8 without BOM", as shown below:
事实证明,答案是我看到的是一个Byte Order Mark,它是一个字符,它告诉正在加载文档的任何东西它是用什么编码的。在我的例子中,它是用 utf-8 编码的,所以相应的BOM 是EF BB BF,如下所示。要删除它,我在Notepad++ 中打开它并单击“Encode in UTF-8 without BOM”,如下所示:
.
.
To actually see the BOM, I had to open it up in TextPadin Binary mode:, and conducted a Google search for "EF BB BF".
要实际查看 BOM,我必须以二进制模式在TextPad中打开它:,并在Google 上搜索“ EF BB BF”。


It took me about 8 hours to find out this was what was causing it, so I thought I'd share this with everyone.
我花了大约 8 个小时才找出导致它的原因,所以我想我会与大家分享这个。
Update: If I had read Joel Spolsky's blog post: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!), then I might not have had this problem.
更新:如果我读过 Joel Spolsky 的博客文章:绝对最低限度,每个软件开发人员绝对,肯定必须知道 Unicode 和字符集(没有借口!),那么我可能不会遇到这个问题。
回答by Benedikt Waldvogel
here's how you do it with vim:
下面是你如何用 vim 做到这一点:
# vim file.xml
:set nobomb
:wq

