0

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.

3
  • There are number of places I will be in need to change the same and those places are not predetermined as well (only identification for those are ${FileID}). Commented May 30, 2012 at 5:38
  • I just found a work around with the following code. But still I feel like there will be more better way. code [CmdletBinding()] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]]$ReplaceFile ); [xml]$ReplaceFileContent = Get-Content $ReplaceFile; $FileID = $ReplaceFileContent.Content.FileID; [String]$ReplaceFileContentString = Get-Content $ReplaceFile; $newReplaceFileContentString = $ReplaceFileContentString.Replace("${FileID}", $FileID); [xml]$newReplaceFileContent = [xml]$newReplaceFileContentString; code` Commented May 30, 2012 at 6:51
  • How is this different from what I provided in my updated answer? Commented May 30, 2012 at 8:54

1 Answer 1

3
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>
Sign up to request clarification or add additional context in comments.

5 Comments

Hi ravikanth, the above way works if only the place where I need to change is predetermined. There are number of places I will be in need to change the same and those places are not predetermined as well (only identification for those are ${FileID}). I am sorry if that was not clear from the question posted.
So, would that also be a part of <add> tag? It will be good if you can share the actual XML content.
${FileID} can come in any node at any level and it can be any number of times as well in the input file. So it can be part of any other node as the way it is part of <add> tag. The XML part in the question is modified just now to make it clear.
But, <FileID> will appear always inside <content> at the same location in XML always? Right?
I updated my answer. If there is no real structured way to find the information, I'd just use the string replacement techniques.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.