1

I'm getting crazy. I need to modify this XML file in powershell

<Smart>
  <Settings>
    <Section name="x">
      <Parameter name="a" value="true" />
      <Parameter name="b" value="0" />
      <Parameter name="c" value="13873" />
      <Parameter name="d" value="true" />
      <Parameter name="e" value="EAI" />
    </Section>
    <Section name="z">
      <Parameter name="h" value="true" />
      <Parameter name="i" value="0" />
      <Parameter name="j" value="13873" />
      <Parameter name="k" value="true" />
      <Parameter name="l" value="EAI" />
    </Section>
  </Settings>
</Smart>

What I want to do is add another line such as:

< Parameter name="f" value="OK" />

But I want to add the new line inside the first Section < Section name="x">

    #Modify XML file
    Write-Host "OPENING XML FILE";
    $path = "\\$computer\$FileName"
    [xml] $xml = Get-Content $path
    
    #return $xml.SmartUpdate.Settings.Section.Parameter

    #set values for the XML nodes you need. This uses the XPath of the value needed.

   **WANT TO ADD THE NEW PARAMETER HERE**

    #Save the file
    $xml.save($path)
    Write-Host "XML FILE SAVED";

I cannot find the solution. Please help me

1
  • You can use msxsl and XSLT stylesheet Commented Feb 26, 2021 at 16:03

2 Answers 2

2

Since you have multiple Section nodes, you need to specify the node to append the new childnode to:


$path = "\\$computer\$FileName"
[xml]$xml = Get-Content -Path $path -Raw

# create a new childnode and append attributes to it
$childNode = $xml.CreateElement("Parameter")
$attrib = $xml.CreateAttribute('name')
$attrib.Value = 'f'
[void]$childNode.Attributes.Append($attrib)
$attrib = $xml.CreateAttribute('value')
$attrib.Value = 'OK'
[void]$childNode.Attributes.Append($attrib)

# select the parent node to append this childnode to
$parentNode = $xml.Smart.Settings.Section | Where-Object { $_.name -eq 'x' }
[void]$parentNode.AppendChild($childNode)

$xml.Save($path)
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this should work. Note that my XML skills with Powershell are far from good so I hope someone else has a better solution

# Create a new node
$NewNode = $Xml.CreateNode([System.Xml.XmlNodeType]::Element,"Parameter",$null)
# create the attributes and set the values
$NameAttribute = $XMl.CreateAttribute('name')
$NameAttribute.Value = 'f'
$ValueAttribute = $Xml.CreateAttribute('value')
$ValueAttribute.Value = 'OK'
# append the attributes
$NewNode.Attributes.Append($NameAttribute)
$NewNode.Attributes.Append($ValueAttribute)
# append the new node to the correct parent
$Xml.Smart.Settings.Section.AppendChild($NewNode)

1 Comment

Hi, Thank you. It worked. What should I do if I had two Sections inside the Settings?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.