名称不能以 ' 开头'特点
我正在用 C# 解析一些 XML。我从数据库获取它,因此在使用 XmlTextReader 读取它之前将其转换为 MemoryStream。问题是我收到此错误:名称不能以 ' ' 字符开头,十六进制值 0x20。第 1 行,位置 3。
以下是我的 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#:
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();
}
}
我认为这可能是编码的问题,但我尝试过UTF8和ASCII,但找不到任何东西。
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?
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#:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的,您应该删除
这里是相关的 XML 规范。
Yes, you should delete the space between <? and xml.
Here's the relevant XML spec.
此错误的另一个常见来源是 XmlReader 尝试将脚本读取为 xml。这是在脚本标记之后开始注释脚本的一个很好的理由。他们仍然会运行:
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:
您的错误消息非常明确,您在第 1 行的 posn 3 处有错误。尝试
-- 没有空格。
Your error message is quite explicit, you have an error at posn 3 in line 1. Try
<?xml
-- no space.删除文档中的第一个空格:
Remove the first space in the document:
我在同一情况下的错误是文件没有保存为 UTF-8。
My error in same case was that file wasn't saved as UTF-8.
您还应该小心并避免使用以下表达式:
名称等于后的空格可能会使您的代码崩溃
You also should be carefull and avoid expressions like:
The blank space that follows the name's equal could make your code crash
我在读取 XML 文件时遇到同样的错误。
事实证明我有一个错误的 <我的文件中的字符。
我注释掉了某些子节点,当删除其中一个注释标签时,我留下了一个额外的 <在文件中。返回错误消息“名称不能以 '\r' 字符开头”,而这个问题是该精确搜索的最佳谷歌结果。
I was getting the same error reading an XML file.
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.
因为这个我犯了很多错误。确保没有空格。我删除了两个对我有用的空格。
是:
什么有效:
这里也有一个空格:< abc:def>。删除 < 周围的所有空格和>。
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:
What worked:
There was a space here too: < abc:def >. Remove all the spaces around the < and the >.
如果在 SSIS 中,只需转到解决方案资源管理器,选择项目,然后从“生成”菜单选项中选择“重建解决方案”。
If in SSIS, just go to Solution Explorer, select the project then select "Rebuild Solution" from the Build menu option.