开发者

Read XML file with BOM in Powershell

Powershell seems to be barfing on an xml file with a unicode BOM - t开发者_JS百科he code:

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

blows up with 'Data at the root level is invalid'.

Is there an easy way to do this without fiddling around with the contents of the file?


You're trying to convert a script block into XML here. Use () instead of {}:

$xml  = [xml] (gc $filename)

In fact, the error message tells you as much already:

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

You notice how the contents of the script block show up in the error message?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜