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?
精彩评论