名称不能以 ' 开头'特点

发布于 2024-08-21 21:37:20 字数 956 浏览 16 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

妄断弥空 2024-08-28 21:37:20

是的,您应该删除

<?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>

这里是相关的 XML 规范。

Yes, you should delete the space between <? and 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>

Here's the relevant XML spec.

素手挽清风 2024-08-28 21:37:20

此错误的另一个常见来源是 XmlReader 尝试将脚本读取为 xml。这是在脚本标记之后开始注释脚本的一个很好的理由。他们仍然会运行:

<script language="javascript" type="text/javascript">
<!--
    function myFunction() {
    }
    ...
-->
</script>

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:

<script language="javascript" type="text/javascript">
<!--
    function myFunction() {
    }
    ...
-->
</script>
梨涡少年 2024-08-28 21:37:20

您的错误消息非常明确,您在第 1 行的 posn 3 处有错误。尝试 -- 没有空格。

Your error message is quite explicit, you have an error at posn 3 in line 1. Try <?xml -- no space.

寂寞清仓 2024-08-28 21:37:20

删除文档中的第一个空格:

<?xml version="1.0" encoding="utf-8"?>

Remove the first space in the document:

<?xml version="1.0" encoding="utf-8"?>
梦巷 2024-08-28 21:37:20

我在同一情况下的错误是文件没有保存为 UTF-8。

My error in same case was that file wasn't saved as UTF-8.

潦草背影 2024-08-28 21:37:20

您还应该小心并避免使用以下表达式:

<e order="0" type="custom" name= "test">

名称等于后的空格可能会使您的代码崩溃

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

清秋悲枫 2024-08-28 21:37:20

我在读取 XML 文件时遇到同样的错误。

事实证明我有一个错误的 <我的文件中的字符。

我注释掉了某些子节点,当删除其中一个注释标签时,我留下了一个额外的 <在文件中。返回错误消息“名称不能以 '\r' 字符开头”,而这个问题是该精确搜索的最佳谷歌结果。

<node>
    <!-- <child /> --><
    <child />
    <child />
</node>

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.

<node>
    <!-- <child /> --><
    <child />
    <child />
</node>
攒一口袋星星 2024-08-28 21:37:20

因为这个我犯了很多错误。确保没有空格。我删除了两个对我有用的空格。

是:

xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"

什么有效:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

这里也有一个空格:< 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:

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 >.

深陷 2024-08-28 21:37:20

如果在 SSIS 中,只需转到解决方案资源管理器,选择项目,然后从“生成”菜单选项中选择“重建解决方案”。

If in SSIS, just go to Solution Explorer, select the project then select "Rebuild Solution" from the Build menu option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文