DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Searching and updating XML file in PowerShell  (Read 14,561 times)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Searching and updating XML file in PowerShell
« on: March 22, 2022, 12:54 PM »
I have the following bit in a Powershell script:

Code: ( powershell)
[xml]$xmlConfig = Get-Content "odata.xml"
$validNodes = $xmlConfig | Select-Xml -XPath '//variable' | %{$_ | ?{((($_.Node.Property.Name -eq "id") -and ($_.Node.Property.Value -eq "this-is-the-node")))}}
if ($validNodes.Count -gt 0) 
{ 
    $validNodes.development = $encryptedcredentials
}

And it's not returning valid nodes, even though the node is there.

The XML file looks like the following:

Code: ( xml)
<configuration>
  <variable>
    <id>this-is-not-the-node</id>
    <development>2</development>
    <staging>0</staging>
    <production>0</production>
  </variable>
  <variable>
    <id>this-is-the-node</id>
    <development>2</development>
    <staging>0</staging>
    <production>0</production>
  </variable>
  <variable>
    <id>this-is-not-the-node-either</id>
    <development>2</development>
    <staging>0</staging>
    <production>0</production>
  </variable>
</configuration>

I want to select the valid node, then edit the value for development under that node.

Any ideas what I'm doing wrong?

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Searching and updating XML file in PowerShell
« Reply #1 on: March 22, 2022, 09:28 PM »
Wouldn't it be easier to use a simple xpath expression to extract the matching element(s)?
Code
Select-Xml -XPath "//variable[id='this-is-the-node']"
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Searching and updating XML file in PowerShell
« Reply #2 on: March 22, 2022, 10:59 PM »
The problem with that is that I don't want that node, I want the parent variable node so I can update the development value. I was under the impression that wouldn't help me, but I'll try it.

So I tried it, and it did indeed give me the subsection I was looking for- how would I then get the development subnode to change it?

And thanks for the help... this has been stumping me for a few days.

UPDATE: I think I found out how to modify the node, but even though when I expand it shows the node as modified, when I save, it never gets to the file.

Code: ( powershell)
[xml]$xmlConfig = Get-Content "odata.xml"
$validNodes = $xmlConfig | Select-Xml -XPath '//variable[id="odata-keys"]' 
if ($validNodes.Count -gt 0) 
{ 
  $validNodes.Node.development = "0" 
  $xmlConfig.Save("odata.xml")
}

What am I doing wrong now?
« Last Edit: March 23, 2022, 01:53 AM by wraith808 »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Searching and updating XML file in PowerShell
« Reply #3 on: March 23, 2022, 06:26 PM »
In case anyone else is wondering, the problem was the assumption on the path.

I added

Code: ( powershell)
$outPath = (Resolve-Path odata.xml).Path
# saves odata.xml in your current working directory
$xmlConfig.Save($outPath)

and it started working.

Thanks for your help!

Original address: https://www.donationcoder.com/forum/index.php?topic=52210.0