1

我想做以下事情:选择Configuration节点,然后根据ObjectName值更改ConfigurationString节点值。

XML 如下:

<DTS:Executable xmlns:DTS="www.microsoft.com/.." DTS:ExecutableType="SSIS.Package">  
  <DTS:Configuration>
    <DTS:Property DTS:Name="ConfigurationType">1</DTS:Property>
    <DTS:Property DTS:Name="ConfigurationString">change me</DTS:Property>
    <DTS:Property DTS:Name="ObjectName">Configuration_1</DTS:Property>
    <DTS:Property DTS:Name="DTSID">{..}</DTS:Property>
  </DTS:Configuration>
  <DTS:Configuration>
    <DTS:Property DTS:Name="ConfigurationType">1</DTS:Property>
    <DTS:Property DTS:Name="ConfigurationString">me to please</DTS:Property>
    <DTS:Property DTS:Name="ObjectName">Configuration_2</DTS:Property>
    <DTS:Property DTS:Name="DTSID">{..}</DTS:Property>
  </DTS:Configuration>

当只有一个此类节点的实例时,我有以下代码来更改 ConfigurationString。

$item = [xml](Get-Content -Path($item_path))
$item.Executable.Configuration.Property | ? { $_.name -eq 'configurationstring'} | % { $_.'#text' = "text" }
$item.Save( $item_path )

我试图添加一个条件? { $_.name -eq 'configurationstring'}来检查是否ObjectName是所需的条件,但我无法返回Configuration节点并更改ConfigurationString节点值。

我也尝试过使用该SelectSingleNode方法,但没有奏效:

$item.SelectSingleNode("Executable/Configuration/Property[@ObjectName='Configuration_1']") | ? { $_.name -eq 'configurationstring'} | % { $_.'#text' = "test" }

谢谢并恭祝安康。

4

2 回答 2

0

SelectSingleNode使用获取父级是一个简单的问题

 $xml.Executable.Configuration | % { $_.property } | # Get all of the properties
  ? { $_.name -eq "ObjectName" -and $_."#text" -eq "Configuration_1" } | #Get the one we are looking for 
  % { $_.selectSingleNode("..").Property } | # Get all of it's sibling properties
  ? { $_.name -eq 'configurationstring'} |   # Get the property we want to change
  % { $_.'#text' = "text" }                  # Update property

它可能不是最干净的,但它应该可以完成工作。

于 2013-08-28T12:51:29.577 回答
0
    PS C:\> $item.Executable.Configuration | % { $_.ChildNodes.GetEnumerator() } | ? {$_.Name -eq "ConfigurationString"} | % { $_.'#text' = "sometext"}

$item.Save("C:\Scripts\so\dtsx.xml")
于 2013-08-28T12:52:13.733 回答