XmlSearchReplace.exe /S="search" [/R]="replace" /O=en,ev,an,av /F="C:\Files\*.xml" [/C] [/I] [/W] [/D] [/L] [/P]="C:\Files\params.txt
S - String to search in the xml. If string contains front slash (/) then the entire string must be surrounded by slash double quotes(\"). If " needs to be searched, then use \" with the entire string inside " (optional)
R - String to replace with in the xml. If string contains front slash (/) then the entire string must be surrounded by slash double quotes(\"). If " needs to be searched, then use \" with the entire string inside " (optional)
O - Options for searching. Can have the following values:
ev - element value
en - element name
an - attribute name
av - attribute value
va - value of attribute
ve - value of element
The va and ve options will search attribute/element name to match with search string supplied and will replace the value of the matching attribute/element.

Separate options with comma, semicolon or space

F - File name to perform search and replace on. Must be a valid xml file. Supports common windows wild cards * and ?. If wild cards are specified, searches all sub directories.
C - Continue on error when processing multiple files. (optional)
I - Ignore case. Default is case sensitive search. (optional)
W - Whole word search. When searching values, whole word means the entire value. (optional)
D - Recurse sub directories. (optional)
L - Replace the search string by a lower case version of the search string. (optional)
P - Specify file name containing multiple search and replace strings. Each line in file should contain a search and replace string. Only following parameters are accepted /S, /R and /L. Syntax is same as command line. (optional)



Example:

library.xml

<Library>
  <Books>
    <Book id="1">
      <Title>Refactoring: Improving the Design of Existing Code</Title>
      <Author>Martin Fowler</Author>
    </Book>
    <book id="1">
      <Title>Agile Principles, Patterns, and Practices in C#</Title>
      <Author>Martin C. Robert, Martin Micah</Author>
    </book>
  </Books>
</Library>


To replace all element names with the value "Book" with "BookInLibrary" on the file library.xml use the following command:
XmlSearchAndReplace.exe /O=en /S=Book /R=BookInLibrary /F=library.xml
Or
XmlSearchAndReplace.exe /O=en /S="Book" /R="BookInLibrary" /F="library.xml"

This will replace only the element named 'Book' but not the one named 'book'. To replace both, add the /I option to the above command line.
Similarly, you can use /O=ev,en to search for the text in both element name and value or /O=ev,av to search in element and attribute values.

If you use /F=*.xml - it will search through the directory and replace all files containing the extension xml in current directory and subdirectories.

To use front slash (/) in search and replace strings, use the following syntax:
XmlSearchAndReplace.exe /O=en /S="cars/trucks" /R="Cars/Trucks" /F="library.xml"

To use a double quote in either search or replace strings, use the following syntax:
XmlSearchAndReplace.exe /O=en /S="trucks with \"double\" chamber" /R="trucks with \"Double\" chamber" /F="library.xml"

To replace the search string with lower case, use the /L option.

XmlSearchAndReplace.exe /O=en /S=Book /F=library.xml /L will replace all string Book with book. The /R option if provided with the /L string will be ignored.

To specify multiple search and replace strings, you can optionally specify a parameter file using the /P option. The parameter file supports only /S, /R and /L options. If a parameter file is specified, then /S, /R and /L options in command lines are ignored. The parameter file has the same syntax as the command line. Each line in a parameter file should contain 1 /S /R or /S /L pair.

XmlSearchAndReplace.exe /O=en /F=library.xml /P=c:\list.txt will take all search and replace parameters from the paramter file c:\list.txt

Example content of list.txt

/S=Hello /L
/S=world /R=WORLD



To replace the value of an element that matches search string with the replace string use the following:

XmlSearchAndReplace.exe /O=ve /S=Title /R=Magic /L will replace all value of elements with the name Title with with word Magic replacing existing values.

Prior to replacement, a backup of the file is taken.

Make sure the files you are trying to replace are not read only.

Last edited Sep 11, 2014 at 8:35 AM by xertrex, version 10

Comments

xertrex Sep 11, 2014 at 9:08 AM 
Hi bugmenot2,

The commandline should look like this:

XmlSearchReplace.exe /O=av /F="path to your config file" /P="path to your param file"

The param file should look like this:

/S="[$WebSite]/MyXYWebSite" /R="MyIISApplication"
/S="[$ConnectionString]" /R="Connection string=connection;DBConn=mydb"
/S=[$CrmDomain] /R=MyCRMDomain

Hope this helps...

bugmenot2 Nov 27, 2013 at 10:16 AM 
Hi there!
Could you please help me with the solution:

I am having the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<parameters>
<setParameter name="IIS Web Application Name" value="[$WebSite]/MyXYWebSite" />
<setParameter name="ConnectionString" value="[$ConnectionString]" />
<setParameter name="Crm domain" value="[$CrmDomain]" />
</parameters>

Is there a way to search and replace from search_and_replace.txt file to become:

<?xml version="1.0" encoding="utf-8"?>
<parameters>
<setParameter name="IIS Web Application Name" value="MyIISApplication" />
<setParameter name="ConnectionString" value="Connection string=connection;DBConn=mydb" />
<setParameter name="Crm domain" value="MyCRMDomain" />
</parameters>

Thanks!!!

Brgds,
S.