简体   繁体   中英

Read and Edit XML content and get output to an XML variable in PowerShell

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.

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>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM