在Powershell中使用BOM读取XML文件

Sam*_*Sam 3 xml powershell byte-order-mark

Powershell似乎是在使用unicode BOM的xml文件上进行抨击 - 代码:

$xml = [xml]{ get-content $filename }
Run Code Online (Sandbox Code Playgroud)

炸毁"根级数据无效".

有没有一个简单的方法来做到这一点,而不是摆弄文件的内容?

Joe*_*oey 6

您正在尝试将脚本块转换为XML.使用()而不是{}:

$xml  = [xml] (gc $filename)
Run Code Online (Sandbox Code Playgroud)

事实上,错误消息已经告诉你了:

PS Home:\> $xml = [xml]{gc test.xml}
Cannot convert value "gc test.xml" to type "System.Xml.XmlDocument". Error: "Data at the root level is invalid. Line 1,
 position 1."
At line:1 char:13
+ $xml = [xml] <<<< {gc test.xml}
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException
Run Code Online (Sandbox Code Playgroud)

您会注意到脚本块的内容如何显示在错误消息中?