I need to add a new element to existing nodes in an XML. The current structure essentially looks like this:
<CommandManagerResults>
<ListReports>
<Row>
<Name>aaa</Name>
</Row>
<Row>
<Name>bbb</Name>
</Row>
</ListReports>
<ListDocuments>
<Row>
<Name>ccc</Name>
</Row>
<Row>
<Name>ddd</Name>
</Row>
</ListDocuments>
</CommandManagerResults>
I need to add an element in all the "Row" nodes. The code I have so far:
$directory = "E:\temp"
cd $directory
[xml]$XmlDocument = Get-Content ".\test.xml"
$ProjectName = $XmlDocument.CreateElement("ProjectName")
$ProjectName.InnerText = "test"
$temp = $XmlDocument.SelectNodes("//Row")
foreach ($row in $temp){
$row.AppendChild($ProjectName)
$XmlDocument.Save($directory + '\test.xml')
}
However, only the very last "Row" node is getting saved with the new "ProjectName" element. I added '$row | FL' inside the foreach loop and it shows each Row as having the ProjectName element as being there through each iteration of the loop, unfortunately no matter if I save inside or after the foreach loop only the last Row node is saving with the ProjectName element. How it's coming out:
<CommandManagerResults>
<ListReports>
<Row>
<Name>aaa</Name>
</Row>
<Row>
<Name>bbb</Name>
</Row>
</ListReports>
<ListDocuments>
<Row>
<Name>ccc</Name>
</Row>
<Row>
<Name>ddd</Name>
<ProjectName>Test</ProjectName>
</Row>
</ListDocuments>
</CommandManagerResults>
What I want the structure to look like in the end:
<CommandManagerResults>
<ListReports>
<Row>
<Name>aaa</Name>
<ProjectName>Test</ProjectName>
</Row>
<Row>
<Name>bbb</Name>
<ProjectName>Test</ProjectName>
</Row>
</ListReports>
<ListDocuments>
<Row>
<Name>ccc</Name>
<ProjectName>Test</ProjectName>
</Row>
<Row>
<Name>ddd</Name>
<ProjectName>Test</ProjectName>
</Row>
</ListDocuments>
</CommandManagerResults>
FYI, very new to Powershell and XMLs so hopefully everything I've said makes sense and I'm at least heading down the right path.