Edit outerxml
I would like to search an xml file and add a new line after the text I'm searching for.  The part I would like to edit is part of the outerxml portion of the xml file.  Can someone help?  Thank you
August 28th, 2015 1:45pm

Hi,

can u post a little example xml?

There are many possible ways to do this - regex - using xml functions by loading the file as [xml] etc.

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 3:04pm

You cannot add lines to an XML file.  YOU can only add elements and attributes and text nodes. (comments too)

August 29th, 2015 12:26am

Would editing the file as a text file work or would it mess up the xml format of the file?  I can add the line if I edit the file as a text file.

$Filename = 'C:\myfile.xml'

(Get-Content $fileName) | 
    Foreach-Object {
        $_
        if ($_ -match "myvalue1234") 
        {
            "Mynewvalue5678"
        }
    } | Set-Content $fileName

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 9:40am

Herfore you could also use replace.

$Filename = 'C:\myfile.xml'

(Get-Content $fileName).Replace("myvalue1234","Mynewvalue5678") | Set-Content $fileName
If you dont make any errors to the xml-structure you can modify the file as you like. Though if you make any errors the xml-file wont be read accurately.
August 31st, 2015 9:45am

Would editing the file as a text file work or would it mess up the xml format of the file?  I can add the line if I edit the file as a text file.

$Filename = 'C:\myfile.xml'

(Get-Content $fileName) | 
    Foreach-Object {
        $_
        if ($_ -match "myvalue1234") 
        {
            "Mynewvalue5678"
        }
    } | Set-Content $fileName

"match" will replace everything  on a line and many XML files are only one line with only a CR. 

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 11:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics