Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取和编辑XML内容,并将其输出到PowerShell中的XML变量_Xml_Powershell_Powershell 2.0 - Fatal编程技术网

读取和编辑XML内容,并将其输出到PowerShell中的XML变量

读取和编辑XML内容,并将其输出到PowerShell中的XML变量,xml,powershell,powershell-2.0,Xml,Powershell,Powershell 2.0,我有一个类似于以下内容的XML文件: <?xml version="1.0" encoding="utf-8"?> <Content> <FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID> <File Path="C:\test.config"> <Tag TagName="configuration"> <

我有一个类似于以下内容的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>

109F2AEA-6D9C-4127-963A-9C71D4155C5D
...
我只需要获取这个XML文件内容并编辑(替换存在${FileID}的地方)行,如下所示

<add fileID="${FileID}"/>

使用下一行中的“FileID”值,并将输出获取为数据类型为[xml]的变量

<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>
109F2AEA-6D9C-4127-963A-9C71D4155C5D
请建议在PowerShell中执行此操作的最佳方法。

PS H:\>$xml=[xml]'
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>
109F2AEA-6D9C-4127-963A-9C71D4155C5D ' $xml.InnerXml.Replace('${FileID}',$xml.Content.FileID) 109F2AEA-6D9C-4127-963A-9C71D4155C5D
PS H:\>$xml=[xml]'
109F2AEA-6D9C-4127-963A-9C71D4155C5D
'
$xml.InnerXml.Replace('${FileID}',$xml.Content.FileID)
109F2AEA-6D9C-4127-963A-9C71D4155C5D

Hi ravikanth,如果我需要改变的地方是预先确定的,那么上述方法就行了。有很多地方我需要更改,这些地方也不是预先确定的(这些地方的唯一标识是${FileID})。我很抱歉,如果这不是从张贴的问题清楚。那么,这也是标签的一部分吗?如果您可以共享实际的XML内容就好了。${FileID}可以在任何级别的任何节点中出现,也可以在输入文件中出现任意次数。因此,它可以是任何其他节点的一部分,就像它是标记的一部分一样。刚才对问题中的XML部分进行了修改,以澄清问题。但是,在XML中,是否总是在同一位置出现?对吧?我更新了我的答案。如果没有真正结构化的方法来查找信息,我只会使用字符串替换技术。嗨,ravikanth,如果我需要更改的位置是预先确定的,那么上面的方法是有效的。有很多地方我需要更改,这些地方也不是预先确定的(这些地方的唯一标识是${FileID})。我很抱歉,如果这不是从张贴的问题清楚。那么,这也是标签的一部分吗?如果您可以共享实际的XML内容就好了。${FileID}可以在任何级别的任何节点中出现,也可以在输入文件中出现任意次数。因此,它可以是任何其他节点的一部分,就像它是标记的一部分一样。刚才对问题中的XML部分进行了修改,以澄清问题。但是,在XML中,是否总是在同一位置出现?对吧?我更新了我的答案。如果没有真正结构化的方法来查找信息,我只会使用字符串替换技术。我需要更改相同的位置,并且这些位置也不是预先确定的(这些位置的唯一标识是${FileID})。我只是找到了一个解决以下代码的方法。但我还是觉得会有更好的办法<代码>代码[CmdletBinding()]参数([parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$ReplaceFile);[xml]$ReplaceFileContent=获取内容$ReplaceFile$FileID=$ReplaceFileContent.Content.FileID;[String]$ReplaceFileContentString=获取内容$ReplaceFile$newReplaceFileContentString=$ReplaceFileContentString.Replace(“
$
{FileID
},$FileID”);[xml]$newReplaceFileContent=[xml]$newReplaceFileContentString
code`这与我在更新的答案中提供的有什么不同?我需要更改相同的位置,并且这些位置也不是预先确定的(这些位置的唯一标识是${FileID})。我刚刚找到了使用以下代码的解决方法。但我还是觉得会有更好的办法<代码>代码[CmdletBinding()]参数([parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$ReplaceFile);[xml]$ReplaceFileContent=获取内容$ReplaceFile$FileID=$ReplaceFileContent.Content.FileID;[String]$ReplaceFileContentString=获取内容$ReplaceFile$newReplaceFileContentString=$ReplaceFileContentString.Replace(“
$
{FileID
},$FileID”);[xml]$newReplaceFileContent=[xml]$newReplaceFileContentString
code`这与我在更新的答案中提供的有什么不同?