Reputation: 3826
I have a PowerShell script that reads very large XML files and extracts the text values of certain elements which represent embedded C# code. The script then saves the element texts into .cs code files in a directory structure which matches the structure of the XML file. For performance, I'm using XmlReader since the files are so large (sometimes >200 MB).
The original XML file has Windows line endings (CRLF) but the .NET Framework XmlReader class "normalises" the line endings from CRLF to just LF (in line with XML specifications) when it reads from the file.
My question is: how can I force PowerShell to restore the line endings to Windows standard when it writes out the file out to disk, so I don't get source control "noise" resulting from changes in line endings?
I'm currently writing the files by using the following PowerShell statement: New-Item -Path $path -Force -ItemType file -Value $reader.ReadElementString()
The $reader
variable is an instance of [System.Xml.XmlReader]
.
Upvotes: 1
Views: 1080
Reputation: 26300
You can do this:
$reader.ReadElementString().Replace("`n","`r`n")
Upvotes: 1
Reputation: 200343
I'd recommend to drop System.Xml.Reader
entirely. PowerShell's built-in XML handling is far easier to manage:
[xml]$data = Get-Content "C:\foo.xml"
$data.SelectNodes("//foo") | % {
$_.bar
$_.baz
} | Out-File "C:\out.txt"
Upvotes: 0