Solved while echoing text from one xml to another ! is not copied

May 9, 2019 at 05:25:29
Specs: Windows 10
for /f "tokens=1* delims=" %%i in (test.xml) do (
echo.%%i >> test1.xml
if "%%i" == "<xxx>" (
echo.^<testing^> >>test1.xml
echo.^<testing/^> >>test1.xml

file: test.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Testing file-->

<?xml version="1.0" encoding="UTF-8"?>
<-- Testing file-->

as you can see "!" is removed while echoing the comments into new file. can someone please help.
Thanks in advance

See More: while echoing text from one xml to another ! is not copied

Reply ↓  Report •

May 9, 2019 at 06:49:10
✔ Best Answer
That'll happen if you have delayed expansion enabled. Which is another reason in the big list of why you shouldn't use batch script for XML editing. You should use VBScript or, if you have Win10, PowerShell instead. If just because it'd keep you from breaking an initially valid XML file, which is what your script would do. Assuming a valid input XML, PowerShell would look like:
$xml = [xml](gc test.xml)
$xml.SelectNodes('//xxx') |
 ForEach-Object {
   $newNode = $xml.CreateElement('testing')
   $_.InsertBefore($newNode, $_.FirstChild)
$writer = New-Object IO.StreamWriter 'test1.xml', $false, ([Text.Encoding]::UTF8)

How To Ask Questions The Smart Way

message edited by Razor2.3

Reply ↓  Report •

May 9, 2019 at 08:22:50
Thanks Razor, removed delayed expansion and it works. need to start an application after adding the line in xml so need to be done in batch.

Reply ↓  Report •
Related Solutions

Ask Question