在Powershell中使用BOM读取XML文件

时间:2011-05-10 08:20:22

标签: xml powershell byte-order-mark

Powershell似乎正在使用unicode BOM对xml文件进行抨击 - 代码:

$xml = [xml]{ get-content $filename }

爆炸,“根级别的数据无效”。

有没有一种简单的方法可以在不摆弄文件内容的情况下做到这一点?

1 个答案:

答案 0 :(得分:6)

您正在尝试将脚本块转换为XML。使用()代替{}

$xml  = [xml] (gc $filename)

实际上,错误消息已经告诉您:

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

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

相关问题