If I have a file with XML like:
<?xml version="1.0" encoding="utf-8"?>
<package date-created="19/07/2016" version="6" system="6">
<description />
<system />
<res id="1057002654" name="ABC" path="/DEF" type="F" mimeType="application/x-shared">
<description />
<keywords />
<resver id="1057014163" major="1" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057014677" major="2" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057015106" major="3" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057016183" major="4" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057016374" major="5" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057017622" major="6" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057017704" major="7" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057017863" major="8" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
</res>
</package>
There could be hundreds of items, and within them there could be one or more . In Powershell, I'm trying to look for the last resver id value where the res id equalls 1057002654.
I started off with this:
$path = 'C:\TEST\package.xml'
$myxml = [xml](Get-Content $path)
$node = $myxml.package.res.resver | where {$_.id -eq "1057002654"}
$node.id = '123'
$myxml.Save($path)
This was just a quick test to see if I could get to the bit I wanted and update it, but that fails with:
Property 'id' cannot be found on this object; make sure it exists and is settable.
I then tried something like:
$xml = [xml](Get-Content 'C:\TEST\package.xml')
$nodes = $xml.SelectNodes("//*[@res]")
foreach ($node in $nodes) {
$node.Attributes | %{$_} > C:\TEST\disk.txt }
This actually writes all the values in the attributes to a .txt file but cannot seem to find a way to actually do what I want.
/Edit - Corrected with what I actually meant to do. Ended up with code like this, where $Ref is an item in the array $References
foreach ($Ref in $References)
{
$xml = [xml](Get-Content 'C:\TEST\package.xml')
$res = $xml.SelectSingleNode('//res[@id = $Ref]/child::resver')
$resource = $res.id + ".txt"
# more stuff here
}
Getting this error though:
Exception calling "SelectSingleNode" with "1" argument(s): "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."
At C:\TEST\script.ps1:22 char:38
+ [string] $res = $xml.SelectSingleNode <<<< ('//res[@id = $Ref]/child::resver')
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException