获得 "hexadecimal value 0x3C, is an invalid attribute character" 正确的日语 XML

Getting "hexadecimal value 0x3C, is an invalid attribute character" in correct Japanese XML

所有,我得到

hexadecimal value 0x3C, is an invalid attribute character

尝试在具有日语语言环境和文件中的日语字符的计算机上读取 XML 时出错:

  $xml = [xml]@(Get-Content $file)

XML 中的特定位置非常良性:

<Control type="select">

据我所知,几乎所有此类错误都是由字符串中某处的“<”引起的。所以我检查了我的 XML 并没有在不应该出现的地方看到任何额外的“<”。 但后来我发现一个 hotfix description 是这样说的:

Consider the following scenario: You have a computer that is running Windows Server 2012. The system locale setting of the computer is set to Japanese. You install the Network Policy and Access Services (NPAS) server role with default settings on the computer. You try to run the Best Practices Analyzer tool to scan Network Access Protection (NAP) components.

听起来有点耳熟,所以我怀疑这是由于日语语言环境或 XML.

的某些值中的日语字符所致

文件本身是这样开始的:

<?xml version="1.0" encoding="utf-8"?>

有什么想法吗?

在这种情况下有用的是在获取文件内容时明确指定编码:

 $xml = [xml]@(Get-Content $file -Encoding UTF8)

这是一个如何实现的演示。这个 file.xml 有一个用 utf8 编码的日文字符 no bom:

<こ/>

那个字符是 unicode 0x3053:

[int][char]'こ' | % tostring x

3053

'こ' | format-hex -Encoding bigendianunicode


   Label: String (System.String) <32532FE4>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 30 53                                           0S

Powershell 5 将假定文件为 ascii:

[xml]$xml = get-content file.xml

Cannot convert value "<ã“/>" to type "System.Xml.XmlDocument". Error: "The '' character,
hexadecimal value 0x81, cannot be included in a name. Line 1, position 3."
At line:1 char:1
+ [xml]$xml = get-content file.xml
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

Unicode 字符 0x3053 将在 utf8 中编码为 3 个字节,"E3 81 93",因为它所在的范围。utf8 位前缀如下所示 (https://en.wikipedia.org/wiki/UTF-8)。三个字节始终以“1110”或 'E' 开头。其他“10”位继续它。

1110xxxx    10xxxxxx    10xxxxxx

Xml 不介意 E3,因为它是一个上面有波浪线的 a,但 81 看起来就像一个空框(Whosebug 无法显示它),所以它会导致错误.这是 file.xml 的原始字节。 “0D 0A”只是回车 return 和换行。

format-hex file.xml -encoding utf8


           Path: C:\Users\admin\foo\file.xml

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   3C E3 81 93 2F 3E 0D 0A                          <ã“/>..

我不知道如何准确重现 op 的错误,但大致就是这样。