【问题标题】:Read and Edit XML content and get output to an XML variable in PowerShell读取和编辑 XML 内容并将输出输出到 PowerShell 中的 XML 变量
【发布时间】:2012-05-30 16:14:12
【问题描述】:

我有一个类似于以下内容的 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 文件内容并编辑(替换任何 ${FileID} 存在的地方)如下所示的行

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

使用下一行中“FileID”的值并将输出获取到 DataType [xml] 的变量。

<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>

请建议可以在 PowerShell 中完成此操作的最佳方式。

【问题讨论】:

  • 我需要更改的地方有很多,这些地方也没有预先确定(只有 ${FileID} 的标识)。
  • 我刚刚找到了使用以下代码的解决方法。但我仍然觉得会有更多更好的方法。 code [CmdletBinding()] 参数 ( [参数(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`
  • 这与我在更新答案中提供的内容有何不同?

标签: xml powershell powershell-2.0


【解决方案1】:
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>

【讨论】:

  • 嗨 ravikanth,如果只预先确定了我需要更改的地方,上述方法就有效。有很多地方我需要更改相同的地方,这些地方也不是预先确定的(这些地方的唯一标识是 ${FileID})。如果发布的问题不清楚,我很抱歉。
  • 那么,这也是 标签的一部分吗?如果您可以共享实际的 XML 内容,那就太好了。
  • ${FileID} 可以在任何级别的任何节点中出现,也可以在输入文件中出现任意次数。所以它可以是任何其他节点的一部分,就像它是 标记的一部分一样。刚才对问题中的XML部分进行了修改,说明清楚。
  • 但是, 总是会出现在 中的 XML 中的同一位置吗?对吗?
  • 我更新了我的答案。如果没有真正的结构化方式来查找信息,我只会使用字符串替换技术。
猜你喜欢
  • 2011-09-02
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多