十六进制值为0x00是无效字符 [英] Hexadecimal value 0x00 is a invalid character

查看:5515
本文介绍了十六进制值为0x00是无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成一个XML文档从一个StringBuilder,基本上是这样的:

<$p$p><$c$c>string.Format("<text><row>{0}</row><col>{1}</col><textHeight>{2}</textHeight><textWidth>{3}</textWidth><data>{4}</data><rotation>{5}</rotation></text>

之后,是这样的:

  XmlDocument的文档=新的XmlDocument();
document.LoadXml(XML);
XmlNodeList中labelSetNodes = document.GetElementsByTagName(标签);
对于(INT指数= 0;指数&LT; labelSetNodes.Count;指数++)
{
    //做一点事
}
 

所有数据来自一个数据库。 最近,我有一些问题与错误:

  

十六进制值为0x00是无效字符,1号线,位置NNNNN

但其并不一致。 有时,一些空白数据会工作。 在错误的数据适用于某些PC,而不是其他。

在数据库中,数据总是空字符串。这是从来没有'空' 而在XML文件中,它出来为&LT;数据&GT;&LT; /数据&GT; ,即开合之间没有任何字符。 (但不知道这是否可以依靠,因为我从立即窗口向外拉,是可见的工作室并粘贴到textpad)。

有可能是在SQL Server版本的区别(2008年是它会失败,2005年将工作)和整理过。 不知道如果任这些都是可能的原因是什么?

但完全相同的code和数据有时会失败。任何想法问题在哪里?

解决方案

没有实际数据或源,这将是我们很难诊断什么错误。不过,我可以提出几点建议:

  • 在统一code NUL(0×00)在所有版本的XML非法和验证解析器必须拒绝输入包含它。
  • 尽管以上;真实世界的无验证的XML可以包含任何种类的垃圾形成不良字节想象。
  • XML 1.1允许零宽度和非打印控制字符(NUL除外),所以你可以不看在文本编辑器的XML 1.1文件,并告诉它包含了什么字。

鉴于你写的,我怀疑数据库中的数据,以XML断任何转换;它的传播非XML字符。

创建非XML字符(完全无效,德尔斯,控制字符等)的一些数据库条目,并在其上​​运行您的XML转换器。输出的XML文件,并期待它在十六进制编辑器。如果包含非XML字符,你的转换器坏了。修复它,或者,如果你不能,创建preprocessor一个拒绝输出,这样的角色。

如果转换器输出看起来不错,问题是在你的XML消费者;它的地方插入非XML字符。你将不得不打破你的消费过程成为单独的步骤,检查每一步的输出,并缩小东西推出了不好的角色。

I am generating an XML document from a StringBuilder, basically something like:

string.Format("<text><row>{0}</row><col>{1}</col><textHeight>{2}</textHeight><textWidth>{3}</textWidth><data>{4}</data><rotation>{5}</rotation></text>

Later, something like:

XmlDocument document = new XmlDocument();
document.LoadXml(xml);
XmlNodeList labelSetNodes = document.GetElementsByTagName("labels");
for (int index = 0; index < labelSetNodes.Count; index++)
{
    //do something
}

All the data comes from a database. Recently I've had a few issues with the error:

Hexadecimal value 0x00 is a invalid character, line 1, position nnnnn

But its not consistent. Sometimes some 'blank' data will work. The 'faulty' data works on some PCs, but not others.

In the database, the data is always a blank string. It is never 'null' and in the XML file, it comes out as < data>< /data>, i.e. no character between opening and closing. (but not sure if this can be relied on as I am pulling it from the 'immediate' window is vis studio and pasting it into textpad).

There is possibly differences in the versions of sql server (2008 is where it would fail, 2005 would work) and collation too. Not sure if any of these are likely causes?

But exactly the same code and data will sometimes fail. Any ideas where the problem lies?

解决方案

Without your actual data or source, it will be hard for us to diagnose what is going wrong. However, I can make a few suggestions:

  • Unicode NUL (0x00) is illegal in all versions of XML and validating parsers must reject input that contains it.
  • Despite the above; real-world non-validated XML can contain any kind of garbage ill-formed bytes imaginable.
  • XML 1.1 allows zero-width and nonprinting control characters (except NUL), so you cannot look at an XML 1.1 file in a text editor and tell what characters it contains.

Given what you wrote, I suspect whatever converts the database data to XML is broken; it's propagating non-XML characters.

Create some database entries with non-XML characters (NULs, DELs, control characters, et al.) and run your XML converter on it. Output the XML to a file and look at it in a hex editor. If this contains non-XML characters, your converter is broken. Fix it or, if you cannot, create a preprocessor that rejects output with such characters.

If the converter output looks good, the problem is in your XML consumer; it's inserting non-XML characters somewhere. You will have to break your consumption process into separate steps, examine the output at each step, and narrow down what is introducing the bad characters.

这篇关于十六进制值为0x00是无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆