本文介绍了名称不能以''字符开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#解析一些XML.我从数据库中获取它,因此在使用XmlTextReader读取它之前将其转换为MemoryStream.问题是我得到了这个错误:Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1, position 3.以下是我的XML和读取它的代码(好吧,它从数据库中出来,没有空白的第一个字符).有什么建议吗?

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();
    }
}

我以为可能是编码,但是我已经尝试过UTF8和ASCII并找不到任何内容.

I thought it could be the encoding, but I've tried by UTF8 and ASCII and can't find anything.

推荐答案

是的,您应该删除< ;?之间的空格.和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>

此处是相关的XML规范.

Here's the relevant XML spec.

这篇关于名称不能以''字符开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:46