更新子节点及其属性信息-Powershell

时间:2019-12-02 19:17:38

标签: xml powershell

我正在尝试更新服务器上可用的XML文件之一中的macaddress信息。 我看到两种情况

<configuration>
  <serverIdentification id="1d230d28-08bc-4a12-a56a-7dacf7f09282" />
  <NetworkBinding ReSync="false" PublicAddress="dev-000450-02a.gsccloud.ninja" PublicPort="">
   <macAddresses />
  </NetworkBinding>
.
.
.
</configuration>

 <configuration>
      <serverIdentification id="1d230d28-08bc-4a12-a56a-7dacf7f09282" />
      <NetworkBinding ReSync="false" PublicAddress="dev-000450-02a.gsccloud.ninja" PublicPort="">
         <macaddresses>

       </macaddresses>
      </NetworkBinding>
    .
    .
    .
    </configuration>

所需的XMLData应该类似于以下所示。请注意,address(Addr)的值为空。 这将通过其他机制进行更新。所以不用担心。现在,我只希望XML数据如下所示。

<configuration>
          <serverIdentification id="1d230d28-08bc-4a12-a56a-7dacf7f09282" />
          <NetworkBinding ReSync="false" PublicAddress="dev-000450-02a.gsccloud.ninja" PublicPort="">
             <macaddresses>
              <Mac Addr="" />
           </macaddresses>
          </NetworkBinding>
        .
        .
        .
        </configuration>

从我所看到的是,存在子节点“ macAddresses”,但是innerxml数据为空。

 $xmlfilepath  = "C\somepath\path"
    [xml] $XMLData = Get-Content -LiteralPath $xmlfilepath -ErrorAction Stop

enter image description here

对不起,我对XML操作的了解不是那么好。任何信息,将不胜感激。

2 个答案:

答案 0 :(得分:0)

# Read the input file into an XML DOM.
[xml] $xmlData = Get-Content -Raw -LiteralPath $xmlfilepath -ErrorAction Stop

# Target the <macAddresses> element and add the desired child element
# via its .InnerXml property.
# Note the required use of indexing syntax (['macAddresses']).
$xmlData.configuration.NetworkBinding['macAddresses'].InnerXml = '<Mac Addr="" />'

# $xmlData is now updated; inspect with .OuterXml or save to a file
# with, say, $xmlData.Save("$PWD/new.xml")

注意:必须使用 Index 表示法-$xmlData.configuration.NetworkBinding['macAddresses']-而不是 dot表示法-$xmlData.configuration.NetworkBinding.macAddresses才能可靠地定位{{1 }}元素:

PowerShell的点符号 <macAddresses>)通常非常方便,但是 leaf 元素的情况下(完全不包含内容的 或仅包含文本的内容,如果 具有属性,则返回元素的字符串内容而不是元素对象本身XmlElement

  • 可以使用点符号来修改叶元素的文本内容 (例如,.macAddresses

  • 不能使用点表示法来访问元素调用方法叶元素上的(例如,$xmlData.configuration.NetworkBinding.macAddresses = 'foo' 失败-$xmlData.configuration.NetworkBinding.macAddresses.InnerXml = '<foo/>'被视为.macAddresses,并且字符串没有[string]属性)

    • 请注意,-叶子元素总是 本身返回(.InnerXml实例)。

通过对比, 索引符号 XmlElement)是一种本机类型的功能,它还可以通过名称定位子元素,但是一致将它们作为['macAddresses']对象返回,因此您可以访问它们上的属性XmlElement或调用方法.InnerXml

在您的情况下,由于您要向.SetAttribute()元素添加 child 节点,因此需要分配给<macAddresses>属性,因此需要使用索引符号。

答案 1 :(得分:0)

我能够通过其他解决方案实现这一目标。这是我将在函数中使用的代码的片段。

[xml] $XMLData = Get-Content -LiteralPath $xmlfilepath -ErrorAction Stop

$appSettingsXml = @"
<macAddresses>
    <Mac Addr="" />
</macAddresses> 
"@ 


    if(($XMLData.configuration.NetworkBinding.InnerXml -eq "<macAddresses></macAddresses>") -or ($XMLData.configuration.NetworkBinding.InnerXml -eq "<macAddresses />")){
        $XMLData.configuration.NetworkBinding.InnerXml=""
        $XMLData.configuration.NetworkBinding.InnerXml=$appSettingsXml
    }
.
.
.
.
$XMLData.save($xmlfilepath)