调用 Xdocument.save 之前应跳过的字符

发布于 2024-12-29 07:25:45 字数 205 浏览 1 评论 0原文

我有这个问题..我正在从客户端接收数据..使用套接字连接。但这并不重要。所有数据都使用 XDocument 对象存储在 XML 文件中。 有时,当调用 XDocument.Save(filestream) 并向其传递带有特定字符或符号的数据时,我会收到类似(无效的十六进制字符)的异常,这意味着 XML 文件不能包含某些特殊字符? 如果是这样,我在哪里可以找到可以生成此类异常的特殊字符的列表?

I have this problem.. I'm receiving data from a client..Using a socket connection. But this is not important. All the data is stored in an XML file using the XDocument object.
Sometimes when calling the XDocument.Save(filestream) and passing to it data with particular characters or symbols, I get an Exception like (invalid Hexadecimal character), it means that the XML file could not have some special characters ?
If so where can I find a list of this special characters that could generate such Exceptions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甜心小果奶 2025-01-05 07:25:45

[0x0-0x1F] 范围内的字符称为控制字符,除了 0x9、0xA、0xD 之外,它们不能在 XML 中使用。

以下是 XML 规范 的引用:

[定义:已解析的实体包含文本、字符序列、
它可能代表标记或字符数据。] [定义:A
字符是 ISO/IEC 10646:2000 指定的文本原子单位
[ISO/IEC 10646]。合法字符有制表符、回车符、换行符、
以及 Unicode 和 ISO/IEC 10646 的合法字符。
A.1 规范性参考文献中引用的这些标准是最新的
本文件的准备时间。可能会添加新角色
通过修订或新版本的标准。因此,XML 处理器
必须接受为 Char 指定的范围内的任何字符。 ]

字符范围

字符 ::= #x9 | #xA | #xD | [#x20-#xD7FF] |
[#xE000-#xFFFD] | [#x10000-#x10FFFF] /* 任何 Unicode 字符,
不包括代理块、FFFE 和 FFFF。 */

Characters in the range [0x0-0x1F] are called control characters and excluding 0x9, 0xA, 0xD they cannot be used in XML.

Here's a quote from the XML specification:

[Definition: A parsed entity contains text, a sequence of characters,
which may represent markup or character data.] [Definition: A
character is an atomic unit of text as specified by ISO/IEC 10646:2000
[ISO/IEC 10646]. Legal characters are tab, carriage return, line feed,
and the legal characters of Unicode and ISO/IEC 10646. The versions of
these standards cited in A.1 Normative References were current at the
time this document was prepared. New characters may be added to these
standards by amendments or new editions. Consequently, XML processors
must accept any character in the range specified for Char. ]

Character Range

Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] |
[#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character,
excluding the surrogate blocks, FFFE, and FFFF. */

单身狗的梦 2025-01-05 07:25:45

@Darin 给出的答案应该能让你指出正确的方向。我要补充的是,如果您有 xml 开发工具,例如 XMLSpyStylus Studio 您可以快速确定有问题的字符。如果您没有这样的工具,您可以使用 WC3 学校提供的在线 xml 验证器:

http ://www.w3schools.com/xml/xml_validator.asp

这将检查 xml 的格式是否正确,如果包含无效字符,则格式不正确。

更不可能的情况可能是 xml 文档指定的编码确实允许导致问题的字符。如果文档被指定为 UTF-8(很可能就是这样),那么这不太可能是问题所在。

The answer given by @Darin should get you pointed in the right direction. I will add that if you have a tool for xml development such as XMLSpy or Stylus Studio you can quickly determine the offending characters. If you don't have such a tool you can use the online xml validator from WC3 schools found here:

http://www.w3schools.com/xml/xml_validator.asp

This will check that the xml is well formed and if it contains invalid characters it will not be well formed.

A more unlikely scenario might be that the encoding specified by the xml document does allow the characters causing the problem. If the document is specified as UTF-8, which it probably is, then this is not likely the problem.

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