Powershell编辑Xml导致Visual Studio报告不一致的行结束

时间:2012-05-25 20:43:27

标签: xml visual-studio-2010 powershell nuget

Powershell初学者在这里..

所以我创建了一个带有PowerShell脚本的Nuget包。 powershell脚本在安装Visual Studio项目时会修改其中的xml架构。问题是......

如果xml文件中有多行块注释,如

<!--<foo>
   <bar>
   </bar>
</foo>-->

运行脚本后,Visual Studio弹出一条消息,说我的文件“行不一致......”

如果没有评论,或者有一行评论如

<!--foo-->

没有问题。不知何故,以块注释结尾的行由powershell函数从CRLF更改为其他内容。

这是我的powershell脚本,用于加载和保存xml文件

 [xml]$xml = gc $xmlFileLocation -encoding utf8

 $xml.Save($xmlFileLocation)

我也尝试了

的内容
 set-content -encoding utf8 $xmlFileLocation $xml.outerXml

但我继续收到这条消息......

任何sugguests / help都会非常感激。

2 个答案:

答案 0 :(得分:7)

管道Get-ContentOut-String

[xml] $xml = Get-Content 'foo.xml' | Out-String
$xml.foo.bar = 'baz'
$xml.Save('foo.xml')

我刚测试了这个:没有Out-String,我在Visual Studio中看到了这条消息。对于Out-String,我不这样做,评论也不在考虑范围内。

答案 1 :(得分:0)

使用Select-Xml cmdlet代替Get-Content

使用: $ xml =(Select-Xml -Path foo.xml -XPath /).Node

参考:https://blog.stangroome.com/2014/02/10/powershell-select-xml-versus-get-content/