Reputation: 2326
I have an XML file content similar to the following:
<?xml version="1.0" encoding="utf-8"?>
<Content>
<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>
<File Path="C:\test.config">
<Tag TagName="configuration">
<add Content="One" fileID="${FileID}"/>
<secondnode Content="Two" fileID="${FileID}">
<nodeinside>
<inneragain fileID="${FileID}"/>
</nodeinside>
</secondnode>
...
<diffentnode Content="Infinite" fileID="${FileID}"/>
</Tag>
</File>
</Content>
I just need to get this XML file content and edit (replacing whereever ${FileID} is present) the lines as shown below
<add fileID="${FileID}"/>
using the value of the "FileID" from the following line and get the output to a variable of DataType [xml].
<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>
Please suggest the best way this can be done in PowerShell.
Upvotes: 0
Views: 680
Reputation: 25810
PS H:\> $xml = [xml]'<?xml version="1.0" encoding="utf-8"?>
<Content>
<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>
<File Path="C:\test.config">
<Tag TagName="configuration">
<add Content="One" fileID="${FileID}"/>
<secondnode Content="Two" fileID="${FileID}">
<nodeinside>
<inneragain fileID="${FileID}"/>
</nodeinside>
</secondnode>
<diffentnode Content="Infinite" fileID="${FileID}"/>
</Tag>
</File>
</Content>'
$xml.InnerXml.Replace('${FileID}',$xml.Content.FileID)
<?xml version="1.0" encoding="utf-8"?><Content><FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID><File Path="C:\test.config"><Tag TagName="configuration"><add Content="One" fileID="109F2AEA-6D9C-4127-963A-9C71D4155C5D" /><secon
dnode Content="Two" fileID="109F2AEA-6D9C-4127-963A-9C71D4155C5D"><nodeinside><inneragain fileID="109F2AEA-6D9C-4127-963A-9C71D4155C5D" /></nodeinside></secondnode><diffentnode Content="Infinite" fileID="109F2AEA-6D9C-4127-963A-9C
71D4155C5D" /></Tag></File></Content>
Upvotes: 3