【问题标题】:Append cloned XML node with different value - Powershell附加具有不同值的克隆 XML 节点 - Powershell
【发布时间】:2021-01-26 22:42:58
【问题描述】:

我有一个格式如下的 XML 文件:

<?xml version="1.0" encoding="UTF-16"?>
<migration urlid="http://www.microsoft.com/migration/1.0/migxmlext/MigExcludeDrives">
  <_locDefinition>
    <_locDefault _loc="locNone" />
    <_locTag _loc="locData">displayName</_locTag>
  </_locDefinition>
  <component type="Documents" context="UserAndSystem">
    <displayName _locID="migexcludedrives.excludedrives">Unconditional Exclude Drives</displayName>
    <role role="Data">
      <rules>
        <unconditionalExclude>
          <objectSet>
            <pattern type="File">something</pattern>
          </objectSet>
        </unconditionalExclude>
      </rules>
    </role>
  </component>
</migration>

我需要根据某个值克隆&lt;component&gt;标签几次,大概是4-5次。

我正在尝试使用以下代码,但我无法附加所有值,而只能附加最终值。

奇怪的是没有错误。

$something = @(1, 2, 3, 4, 5, 5)

foreach($num in $something) {
    $drivething = "D:"
    $xml = [xml](Get-Content -Path "C:\Temp\referenceXml.xml")
    $element = $xml.migration.component.clone()
    $xml.DocumentElement.AppendChild($element)
}

$xml.Save("C:\temp\newXML.xml")

预期输出为:

<?xml version="1.0" encoding="UTF-16"?>
<migration urlid="http://www.microsoft.com/migration/1.0/migxmlext/MigExcludeDrives">
  <_locDefinition>
    <_locDefault _loc="locNone" />
    <_locTag _loc="locData">displayName</_locTag>
  </_locDefinition>
  <component type="Documents" context="UserAndSystem">
    <displayName _locID="migexcludedrives.excludedrives">Unconditional Exclude Drives</displayName>
    <role role="Data">
      <rules>
        <unconditionalExclude>
          <objectSet>
            <pattern type="File">something</pattern>
          </objectSet>
        </unconditionalExclude>
      </rules>
    </role>
  </component>
  <component type="Documents" context="UserAndSystem">
    <displayName _locID="migexcludedrives.excludedrives">Unconditional Exclude Drives</displayName>
    <role role="Data">
      <rules>
        <unconditionalExclude>
          <objectSet>
            <pattern type="File">1</pattern>
          </objectSet>
        </unconditionalExclude>
      </rules>
    </role>
  </component>
  <component type="Documents" context="UserAndSystem">
    <displayName _locID="migexcludedrives.excludedrives">Unconditional Exclude Drives</displayName>
    <role role="Data">
      <rules>
        <unconditionalExclude>
          <objectSet>
            <pattern type="File">2</pattern>
          </objectSet>
        </unconditionalExclude>
      </rules>
    </role>
  </component>
</migration>

但我得到的只是:

<?xml version="1.0" encoding="UTF-16"?>
<migration urlid="http://www.microsoft.com/migration/1.0/migxmlext/MigExcludeDrives">
  <_locDefinition>
    <_locDefault _loc="locNone" />
    <_locTag _loc="locData">displayName</_locTag>
  </_locDefinition>
  <component type="Documents" context="UserAndSystem">
    <displayName _locID="migexcludedrives.excludedrives">Unconditional Exclude Drives</displayName>
    <role role="Data">
      <rules>
        <unconditionalExclude>
          <objectSet>
            <pattern type="File">something</pattern>
          </objectSet>
        </unconditionalExclude>
      </rules>
    </role>
  </component>
  <component type="Documents" context="UserAndSystem">
    <displayName _locID="migexcludedrives.excludedrives">Unconditional Exclude Drives</displayName>
    <role role="Data">
      <rules>
        <unconditionalExclude>
          <objectSet>
            <pattern type="File">5</pattern>
          </objectSet>
        </unconditionalExclude>
      </rules>
    </role>
  </component>
</migration>

我不确定我错过了什么。非常感谢任何帮助。提前致谢

【问题讨论】:

  • 谢谢你的建议,我确实试过了,但问题是它根本不起作用,因为对于第一次迭代它会抛出错误,因为它还不是一个数组,因此,它会抛出错误。

标签: xml powershell usmt


【解决方案1】:

您当前为每个循环迭代重新加载 XML,因此只有最后一个循环迭代才会生效。 XML 应该在循环之前只加载一次。还将$xml.migration.component 保存在一个变量中,这样您就可以始终以相同的方式访问它,即使添加了更多元素(它变成了一个数组)。

$something = @(1, 2, 3, 4, 5, 5)

$xml = [xml]::new()
$xml.Load("C:\Temp\referenceXml.xml")

$component = $xml.migration.component

foreach($num in $something) {
    $drivething = "D:"
    $element = $component.clone()
    $xml.DocumentElement.AppendChild($element)
}

$xml.Save("C:\temp\newXML.xml")

顺便说一句,我已将Get-Content 替换为$xml.Load(),它可以更好地处理XML 编码。

【讨论】:

  • 非常感谢您的解释和解决方案。我刚刚使用了一种变通方法,将组件转换为数组。但是这个解决方案要好得多,所以将使用这个逻辑。再次,非常感谢您
猜你喜欢
  • 2017-03-10
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多