Linq XDocument生成无效的XML
我有这个密码Linq XDocument生成无效的XML,linq,linq-to-xml,Linq,Linq To 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", wcC
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
可能是什么原因造成的?问题是您有一个UTF-8文件,正试图将其验证为ASCII。这两个字节是unicode头。验证程序不支持UTF8/UCS-2。要么将文件保存为ascii(这将中断,因为xml称其为utf-8),要么查找在过去5年内创建的验证器 编辑:
注意:如果要将文件另存为US Ascii,请使用新的XDeclaration(“1.0”、“US Ascii”、“yes”)将文件另存为UTF-8,文件开头带有字节顺序标记字符(该字符以八位字节0xEF开头)
由于某种原因,您的验证器似乎不喜欢此字符。严格来说,这个字符是空白,在XML声明之前加上空白是无效的。但是,我所知道的大多数解析器都会跳过它,将其作为unicode编码的一个指示符,而不会将其视为内容。是的-使用XDeclaration(“1.0”、“us ascii”、“是”),W3C官方标准允许BOM出现在XML声明之前。任何无法处理此问题的验证器都是非标准的。