7

I need to edit a .xml file with powershell. The thing I need to do is to download the .xml file from the server, updated the version number, and then save it in my local computer. This is what I did.

[xml]$myXML = get-content $xmlFileServer
$myXML.'ivy-module'.info.revision = $newVersion
$myXML.Save($newXMLFileName)

Then I will have a new xml file in my local computer. However, I doubt the coding is different since I can't process with this .xml file. The .xml I should get is something like this:

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd" xmlns:e="http://ant.apache.org/ivy/extra">
  <info organisation="XXXX" module="XXXX" revision="2.0.1.0" status="release" publication="20131119202217" />
  <publications>
    <artifact name="XXXX" type="dll" ext="zip" conf="*" />
  </publications>
</ivy-module>

However, after editing with powershell, the .xml contains some hidden info. I tried to open with NotePad++, I got something like this:

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd" xmlns:e="http://ant.apache.org/ivy/extra">
  <info organisation="XXXX" module="XXXX" revision="2.0.1.0" status="release" publication="20131119202217"/>
  <publications>
    <artifact name="XXXX" type="dll" ext="zip" conf="*"/>
  </publications>
</ivy-module>

Can anyone tell me why is the situation? Thank you so much.

3 Answers 3

6

This works for me, based on the link above and on this blog post

$enc = New-Object System.Text.UTF8Encoding( $false )
$wrt = New-Object System.XML.XMLTextWriter( 'c:\path\out.xml', $enc )
$wrt.Formatting = 'Indented'
$myXML.Save( $wrt )
$wrt.Close()

The 'Indented' setting is personal taste; I prefer my XML human readable. The $wrt.Close() is also required.

3
  • NB: This works for me; but to have text editors recognise that the file is UTF-8 and not ANSI I had to use $true for the encoderShouldEmitUTF8Identifier parameter on UTF8Encoding's constructor.
    – JohnLBevan
    Dec 7, 2015 at 10:49
  • This doesn't work for me in PowerShell 5.1. I get "Multiple ambiguous overloads found for 'Save' and the argument count '1'." Aug 9, 2019 at 19:30
  • Only thing that worked for me was using a StreamWriter: stackoverflow.com/a/30266424 Aug 9, 2019 at 19:57
2

Instead of reading the xml using get-content, read the xml using the XmlDocument directly:

function test($path) {

$xDoc = New-Object System.Xml.XmlDocument
$file = Resolve-Path($path)

$xDoc.Load($file)
$xDoc.Save($file) #will save correctly

}

1
  • $xDoc.Load($file) doesn't work if the XML file doesn't already contain a BOM – it actually throws an exception complaining about the missing BOM. Jul 17, 2017 at 20:54
0

"" is the Byte Order Mark for UTF-8. There is a solution for writing a UTF-8 file without the BOM here: Using PowerShell to write a file in UTF-8 without the BOM

2
  • Thanks. This works. I only need to add one line of codes. Thank you.
    – Payson
    Nov 20, 2013 at 18:34
  • 3
    Could you extend the answer to show how to incorporate the linked solution with an XML file? [System.IO.File]::WriteAllLines($MyPath, $myXML, $Utf8NoBomEncoding) simply prints out "System.Xml.XmlDocument". I would rather not have to write it once with $myXML.Save() then reread it in again to save without the BOM. Sep 26, 2014 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.