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
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)
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.