XDocument 生成无效的 XML
我有这段代码
Dim doc As XDocument = New XDocument( _
New XDeclaration("1.0", "utf-8", "yes"), _
New XElement("transaction", _
New XElement("realm", wcRealm), _
New XElement("password", wcPassword), _
New XElement("confirmation_email", wcConfEmail), _
New XElement("force_subscribe", wcSubscribe), _
New XElement("optout", wcOptOut), _
New XElement("command", _
New XElement("type", wcType), _
New XElement("list_id", wcListId), _
From trans As DataRow In table.Rows _
Order By trans("last") _
Select New XElement("record", _
New XElement("email", trans("email")), _
New XElement("first", trans("first")), _
New XElement("last", trans("last")), _
New XElement("company", trans("company")), _
New XElement("address_1", trans("address_1")), _
New XElement("address_2", ""), _
New XElement("city", trans("city")), _
New XElement("state", trans("state")), _
New XElement("zip", trans("zip")), _
New XElement("country", trans("country")), _
New XElement("phone", trans("phone")), _
New XElement("fax", trans("fax")), _
New XElement("custom_source", trans("source")), _
New XElement("custom_vmail_expire_date", "")))))
'' # Save XML document at root.
doc.Save("c:\vj" & saveDate & ".xml")
,可以正常工作并生成正确的 XML 文件,但我通过验证器运行它并收到此错误。
抱歉,我无法验证此文档,因为在第 1 行,它包含一个或多个我无法解释为 us-ascii 的字节(换句话说,找到的字节不是指定字符编码中的有效值)。请检查文件内容和字符编码指示。
错误是:ascii“\xEF”未映射到 Unicode
可能是什么原因造成的?
I have this code
Dim doc As XDocument = New XDocument( _
New XDeclaration("1.0", "utf-8", "yes"), _
New XElement("transaction", _
New XElement("realm", wcRealm), _
New XElement("password", wcPassword), _
New XElement("confirmation_email", wcConfEmail), _
New XElement("force_subscribe", wcSubscribe), _
New XElement("optout", wcOptOut), _
New XElement("command", _
New XElement("type", wcType), _
New XElement("list_id", wcListId), _
From trans As DataRow In table.Rows _
Order By trans("last") _
Select New XElement("record", _
New XElement("email", trans("email")), _
New XElement("first", trans("first")), _
New XElement("last", trans("last")), _
New XElement("company", trans("company")), _
New XElement("address_1", trans("address_1")), _
New XElement("address_2", ""), _
New XElement("city", trans("city")), _
New XElement("state", trans("state")), _
New XElement("zip", trans("zip")), _
New XElement("country", trans("country")), _
New XElement("phone", trans("phone")), _
New XElement("fax", trans("fax")), _
New XElement("custom_source", trans("source")), _
New XElement("custom_vmail_expire_date", "")))))
'' # Save XML document at root.
doc.Save("c:\vj" & saveDate & ".xml")
which works fine a produces the proper XML file BUT I run it through a validator and get this error.
Sorry, I am unable to validate this document because on line 1 it contained one or more bytes that I cannot interpret as us-ascii (in other words, the bytes found are not valid values in the specified Character Encoding). Please check both the content of the file and the character encoding indication.
The error was: ascii "\xEF" does not map to Unicode
What could be causing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您有一个 UTF-8 文件,您正尝试将其验证为 ASCII。这 2 个字节是 unicode 标头。
The problem is that you have an UTF-8 file that you are trying to validate as ASCII. Those 2 bytes are the unicode headers.
验证器不支持 UTF8/UCS-2。要么将文件另存为 ascii(这会损坏,因为 xml 说它是 utf-8),要么找到过去 5 年内创建的验证器。
编辑:
注意:如果要将其另存为 US Ascii,请使用
new XDeclaration("1.0", "us-ascii", "yes")
The validator doesn't support UTF8/UCS-2. Either save the file as ascii (which will break, as the xml says it's utf-8) or find a validator that was created within the last 5 years.
EDIT:
Note: If you want to save it as US Ascii, use
new XDeclaration("1.0", "us-ascii", "yes")
文件保存为 UTF-8,字节顺序标记字符位于开头(该字符以八位字节 0xEF 开头)。
由于某种原因,您的验证者似乎不喜欢这个角色。严格来说,该字符是空格,并且 XML 声明前面有空格是无效的。然而,我知道的大多数解析器都会跳过它,因为它只是 unicode 编码的指示符,而不是将其视为内容。
The file is saved as UTF-8 with the byte-order-marker character at the start (this character begins with the octet 0xEF).
You validator for some reason seems not to like this character. Strictly speaking this character is whitespace and it is invalid to have whitespace preceeding the XML declaration. However, most parsers I know will skip it as being simply an indicator of unicode encoding and not treat it as content.