# find string and replace it XML file

**URL:** <https://forums.powershell.org/t/find-string-and-replace-it-xml-file/505>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/find-string-and-replace-it-xml-file/505 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/find-string-and-replace-it-xml-file/505/1 "2011-12-31T18:30:00Z")

</div>

by fazlook at 2013-01-17 13:46:35

> Hello,  
>   
> I need to find some strings in XML file and replace them with empty "" string. How can I do that and save back the file ?  
>   
> Need to find c/o and replace it with ""  
>   
> I have this so far :  
>   
> $xml = [xml](Get-Content "c:\1.xml")  
>   
>   
> $Contract = $xml.T550.specimen.contract   
> $Contract  
> $annuitantLastName = $Contract[0].annuitantLastName  
> $annuitantLastName  
> if ($annuitantLastName -match "c/o")  
>   
> {  
> here need to replace then save  
>   
> }  
>   
> example of contract field:  
>   
> contractNumber : 2  
> annuitantSIN : 1111111  
> annuitantSalutation :   
> annuitantFirstName : c/oDenise  
> annuitantLastName : whatever  
> annuitantAddress1 : 535 c/oBoul whatever  
> annuitantAddress2 : This That  
> annuitantCity : ummmm  
> annuitantProvince : USc/0  
> annuitantPostalCode : j4534

by DonJ at 2013-01-17 15:37:39

> The XML object will have a Save() method. See [http://msdn.microsoft.com/en-US/library … ve(v=vs.80](http://msdn.microsoft.com/en-US/library/system.xml.xmldocument.save(v=vs.80)).aspx

by fazlook at 2013-01-18 05:51:15

> But how can I replace a string inside a big string ? I used -replace but did not save the result back to my file

by DonJ at 2013-01-18 06:18:21

> When you cast something as [xml], it isn’t a big string. You’ll need to use the methods of the XMLDocument class to obtain the node you’re after and change its value. You won’t use -replace. So something like $Contract[0].annuitantLastName = ‘new value’.

by fazlook at 2013-01-18 06:54:04

> Hi Don, I dont want to replace a value , I just want to delete strings like c/o or d/y …stuff like that…  
>   
> I did rename my xml file as .txt and the following worked :  
> ($xm = Get-Content "g:\1.xml.txt") | Foreach-Object {  
> $\_ -replace "c/o","" `  
> -replace "&",""  
> } | Set-Content ‘g:\1.txt’  
>   
> but when I name it back to 1.xml it wont load …

by DonJ at 2013-01-18 07:37:03

> So, you’re replacing & with a blank. The ampersand is used within XML to escape illegal characters, so it’s possible that in removing the & you’ve broken the XML. Without seeing the entire original XML (please don’t post it here, though) I couldn’t tell you if that was the case or not.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:58pm UTC](https://forums.powershell.org/t/find-string-and-replace-it-xml-file/505/2 "2024-05-16T20:58:18Z")

</div>


