Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Updating XML element

$
0
0

Hi Guys,

I know it must be very simple but I am trying to understand how I can update null element in a XML file using PS. For Eg:

I have a xml file with following structire:

I need to update child element 'class' with value 'Medium' under subParent which has child element ID = C. Sorry if I made this confusing. Anyway, the end result should be somthing like below:

<?xml version="1.0" encoding="utf-8"?>

<Parent>

<SubParent>

<ID>A</ID>

<Class>Top</Class>

</SubParent>

<SubParent>

<ID>B</ID>

<Class />

</SubParent>

<SubParent>

<ID>C</ID>

<Class>Medium</Class>

</SubParent>

</Parent>

 

I started with someting below but need help with setting value in the element :

 

$xmlsrc ="C:\Temp\xml.xml"

 

[xml]$xmldata = Get-Content $xmlsrc

$elmtclass = $xmldata.Parent.Subparent | ? {$_.ID -eq 'C'} | Select Class

 

if (!$elmtclass.Class) {

<# need help here

$newitem = $xmldata.CreateElement("Class") | ? {$_.ID -eq 'C'}

$newitem.set_InnerXML("Medium")

 

#>

$xmldata.Save("$xmlsrc")

}

else {

write-host 'value already set'

}

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles