Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Editing Xml causing Visual Studio to report Inconsistent Line Ending

Powershell beginner here..

So I've created a Nuget package with a powershell script. The powershell script modifies a xml schema inside my Visual Studio project when it gets installed. Problem being....

If there's a multi-line block comment inside the xml file like

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

After the script is run Visual Studio pops up a message and says my file has "inconsistent line ending......"

If there is no comment, or if there is a single line comment like

<!--foo-->

There is no problem. Somehow the line ending in the block comment is changed from CRLF to something else by the powershell functions.

This is my powershell script for loading and saving the xml file

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

 $xml.Save($xmlFileLocation)

I've also tried something along the lines of

 set-content -encoding utf8 $xmlFileLocation $xml.outerXml

But I keep on getting this message...

Any sugguests/help would be much appreciated.

like image 687
JeffreyXu Avatar asked May 25 '12 20:05

JeffreyXu


1 Answers

Pipe Get-Content through Out-String:

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

I just tested this: without Out-String, I see the message in Visual Studio. With Out-String, I don't, and comments are left alone.

like image 115
Roger Lipscombe Avatar answered Nov 15 '22 19:11

Roger Lipscombe