How to parse XML file with command line (cmd / batch)

I have the following file: Testing.Config

Syntax:

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
    <add name="name1" connectionString="user id=id1;password=password1;"/>
    <add name="name2" connectionString="user id=id2;password=password2;"/>
    <add name="name3" connectionString="user id=id3;password=password3;"/>
</connectionStrings>

I need to parse this file and get id and password when I put the value "name = name1"

For instance:

When I write:

 name=name1

Return:

 id=id1
 password=password1

Hi

+4
source share
2 answers
@echo off

set "xml_file=test.xml"
set /p search_for=Enter name:

for /f "skip=2 tokens=3,9 delims=;= " %%a in ('find """%search_for%""" "%xml_file%"') do (

    set "name=%%~a"
    set "pass=%%b"
)

echo name : %name%
echo pass : %pass%

If all connectionStrings are on split lines, and each row is on the same line. Change the locationxml_file


You can also try xpath.bat (the best option for me) is a small script that allows you to get xml values ​​from an xpath expression without using external binaries:

call xpath.bat connection.xml "//add[@name = 'name1']/@connectionString"
+4
source

( ), powershell , script ( Foo.ps1)

param
(
    [Parameter(Mandatory=$true)]
    [string] $ConfigFilePath,

    [Parameter(Mandatory=$true)]
    [string] $Name
)

([xml](Get-Content -LiteralPath $ConfigFilePath)).connectionStrings.add |
Where-Object {$_.name -eq $name} |
ForEach-Object {($_.connectionString -split ' ')[1] -split ';'}

script , .

0

All Articles