In case someone comes looking for this like I did... the xpath.bat linked by npocmaka works great, but not for file one network paths e.g. \server\foo\bar.xml. And I couldn't get Vikus's PS script to work for attributes. So... I managed to turn Vikus's PS into this:
param
(
[Parameter(Mandatory=$true)]
[string] $XMLFile,
[Parameter(Mandatory=$true)]
[string] $Xpath
)
[xml]$xml = Get-Content -Path $XMLFile
$value = Select-XML -xml $xml -xpath $Xpath
$value.ToString()
Which I then collapsed to this for use in a .cmd file:
set psc="(Select-XML -xml ([xml](Get-Content -Path %xmlfile%)) -xpath %xpath%).ToString()"
for /f %%a in ('powershell %psc%') do set myvar=%%a
Though it's worth noting that neither %xmlfile% nor %xpath% can have spaces. That requires escaping all the parentheses so that the powershell command doesn't have to be wrapped in double quotes:
for /F %a in ('powershell ^(Select-XML -xml ^([xml]^(Get-Content -Path "test.xml"^)^) -xpath "/Computers/Computer/Name/@id"^).ToString^(^)') do @echo %a
FWIW, they're all markedly slower that my original fragile monster:
@for /F eol^=^#^tokens^=2^,5^,7^ delims^=^<^>^" %%i in (%xmlfile%) do @(
@if "%targetnodename% ATTR1=" == "%%i" (
set myvar1=%%j
set myvar1=%%k
)
)