Powershell - 将元素添加到 XML 文件,如果

Powershell - add elements to XML file if

我有一个 xml 文件,其中包含容器提供商信息。我需要检查每个 AddressRow 中是否定义了 "AddressType"。如果定义了 AddressRow(3 种类型之一 - HQ、SUB、MAIN),则一切正常。如果不存在 AddressRow,那么我需要添加缺少的 AddressType 元素和相应的描述。

这是示例 XML 文件。其中显示了 2 个提供商。

<?xml version="1.0" encoding="UTF-8"?>
<OrganisationUnits>
  <OrganisationUnitsRow num="1">
    <OrganisationId>001</OrganisationId>
    <OrganisationName>Provider 001</OrganisationName>
    <Addresses>
      <AddressesRow num="1">
        <AddressType>HQ</AddressType>
        <AddressTypeDesc>Headquarters</AddressTypeDesc>
      </AddressesRow>
      <AddressesRow num="2">
        <MainAddress>N</MainAddress>
        <AddressType>SUB</AddressType>
        <AddressTypeDesc>Sub Office</AddressTypeDesc>
      </AddressesRow>
    </Addresses>
  </OrganisationUnitsRow>
  <OrganisationUnitsRow num="2">
    <OrganisationId>002</OrganisationId>
    <OrganisationName>Provider 002</OrganisationName>
    <Addresses>
      <AddressesRow num="1">
        <MainAddress>Y</MainAddress>
        <NonStandardAddress>
          <LocationId>P520851</LocationId>
          <PostCode>G20 9RQ</PostCode>
        </NonStandardAddress>
      </AddressesRow>
      <AddressesRow num="2">
        <MainAddress>N</MainAddress>
        <NonStandardAddress>
          <LocationId>P526750</LocationId>
        </NonStandardAddress>
      </AddressesRow>
    </Addresses>
  </OrganisationUnitsRow>
</OrganisationUnits>

第一个提供者有 2 个地址行,每个地址行的正确地址类型为 SUB 和 HQ。 第二个提供者有 2 个 AddressRows 但没有定义 AN AddressType。因此我的脚本应该添加默认地址类型 "DEF" 和描述 "Default Address".

<AddressesRow num="1">
    <MainAddress>Y</MainAddress>
    <NonStandardAddress>
        <LocationId>P520851</LocationId>
        <PostCode>G20 9RQ</PostCode>
    </NonStandardAddress>
    <AddressType>DEF</AddressType>
    <AddressTypeDesc>Default Address</AddressTypeDesc>
</AddressesRow>   

这是我的代码,似乎没有添加新元素。

$Path = "C:\DEV\sandpit\small.xml"
$xml = [xml](Get-Content $path)

$node = $xml.OrganisationUnits.OrganisationUnitsRow.Addresses.AddressesRow

foreach($AddressType in $node)
{

    if ($AddressType.'#text' -ne "HQ" -or $AddressType.'#text' -ne "SUB" -or $AddressType.'#text' -ne "MAIN" ) 
    {
        $AddType = $xml.CreateElement("AddressType")
        $AddType.set_InnerXML("DEF")
        $AddTypeDesc = $xml.CreateElement("AddressTypeDesc")
        $AddTypeDesc.set_InnerXML("Default Address")

        $xml.OrganisationUnits.OrganisationUnitsRow.Addresses.AddressesRow.AppendChild($AddType, $AddTypeDesc)


    }
}

$xml.Save($path)

我不明白为什么它没有按预期运行 - 它没有添加新元素。

您目前正在针对 AddressRow 节点本身而不是每个 AddressType 子节点的 InnerText 进行测试。

首先要检查AddressType节点是否存在,然后看正文:

foreach($addressRow in $xml.OrganisationUnits.OrganisationUnitsRow.Addresses.AddressesRow)
{
    if(-not $addressRow.Item('AddressType') -or $addressRow.Item('AddressType')."#text" -notin @('HQ','SUB'))
    {
        $newAddressType = $xml.CreateElement('AddressType')
        $newAddressType.InnerText = 'DEF'

        $newAddressTypeDesc = $xml.CreateElement('AddressTypeDesc')
        $newAddressTypeDesc.InnerText = 'Default Location'

        $newAddressType,$newAddressTypeDesc |ForEach-Object {
            $addressRow.AppendChild($_)
        }
    }
}