C# 名称不能以 ' ' 字符开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2282467/
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
Name cannot begin with the ' ' character
提问by Brian Hicks
I'm parsing some XML in C#. I'm getting it from a database, and so converting it to a MemoryStream before reading it with an XmlTextReader. The problem is that I get this error: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1, position 3.Following is my XML and my code for reading it (it's coming out of the database alright, no blank first character). Any suggestions?
我正在用 C# 解析一些 XML。我从数据库中获取它,因此在使用 XmlTextReader 读取它之前将其转换为 MemoryStream。问题是我收到了这个错误:Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1, position 3.以下是我的 XML 和我读取它的代码(它来自数据库,没有空白的第一个字符)。有什么建议?
XML:
XML:
<? xml version="1.0" encoding="utf-8" ?>
<form>
   <e order="0" type="custom" name="test">
      <fi type="text" />
      <o />
   </e>
   <e order="1" type="zip" />
   <e order="2" type="state" />
</form>
C#:
C#:
byte[] byteArray = new byte[formXml.Length];
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byteArray = encoding.GetBytes(formXml);
MemoryStream xmlStream = new MemoryStream(byteArray);
XmlTextReader xmlReader = new XmlTextReader(xmlStream);
while (xmlReader.Read())
{
    if (xmlReader.HasValue)
    {
        returnString += xmlReader.Depth.ToString();
    }
}
I thought it could be the encoding, but I've tried by UTF8 and ASCII and can't find anything.
我认为这可能是编码,但我已经尝试过 UTF8 和 ASCII 并且找不到任何东西。
采纳答案by Otávio Décio
回答by SLaks
Remove the first space in the document:
删除文档中的第一个空格:
<?xml version="1.0" encoding="utf-8"?>
回答by High Performance Mark
Your error message is quite explicit, you have an error at posn 3 in line 1.  Try <?xml-- no space.
您的错误消息非常明确,您在第 1 行的 posn 3 处有错误。尝试<?xml- 没有空格。
回答by CZahrobsky
Another common source of this error is when the XmlReader attempts to read your scripts as xml. That's a good reason to start commenting out scripts after the script tags. They will still run:
此错误的另一个常见来源是当 XmlReader 尝试将您的脚本读取为 xml 时。这是在脚本标签之后开始注释掉脚本的一个很好的理由。他们仍然会运行:
<script language="javascript" type="text/javascript">
<!--
    function myFunction() {
    }
    ...
-->
</script>
回答by Keith Sirmons
I was getting the same error reading an XML file.
我在读取 XML 文件时遇到了同样的错误。
It turned out I had an errant < character in my file.
结果我的文件中有一个错误的 < 字符。
I was commenting out certain child nodes and when erasing one of the comment tags, I left an extra < in the file. The error message came back "Name cannot begin with the '\r' character" and this question was the top google result for that exact search.
我正在注释掉某些子节点,并且在删除其中一个注释标签时,我在文件中留下了一个额外的 <。错误消息返回“名称不能以 '\r' 字符开头”,这个问题是该确切搜索的顶级谷歌结果。
<node>
    <!-- <child /> --><
    <child />
    <child />
</node>
回答by raleign
I had a lot of errors because of this. Make sure you don't have spaces. There are two places I removed spaces that worked for me.
因为这个,我犯了很多错误。确保您没有空格。我删除了两个对我有用的空间。
Was:
曾是:
xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"
What worked:
什么工作:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
There was a space here too: < abc:def >. Remove all the spaces around the < and the >.
这里也有一个空格:< abc:def >。删除 < 和 > 周围的所有空格。
回答by Migol
My error in same case was that file wasn't saved as UTF-8.
我在同一情况下的错误是该文件没有保存为 UTF-8。
回答by Manuel Roldan
You also should be carefull and avoid expressions like:
您还应该小心并避免使用以下表达式:
<e order="0" type="custom" name= "test">
The blank space that follows the name's equal could make your code crash
名称相等后的空格可能会使您的代码崩溃

