【问题标题】:PowerShell doesn't allow me to add XML node to XML file (using PowerShell 5.1.183)PowerShell 不允许我将 XML 节点添加到 XML 文件(使用 PowerShell 5.1.183)
【发布时间】:2021-04-22 10:29:03
【问题描述】:

从我试图将 XML 节点附加到父 XML 元素的一周开始,我就一直坚持这一点。子节点和父节点如下所示:

[xml]$childxml = @"
<ClaimsProvider>
    <Bomain> hey there</Bomain>
    <Name>Login using </Name>
    <TechnicalProfiles>
        <TechnicalProfile Id="MIDC-What">
            <DisplayName> Employee</DisplayName>
            <Description>Login with your account</Description>
            <Protocol Name="OpenIdConnect" />
            <Metadata>
                <Item Key="METADATA">https://login.microsoftonline.com</Item>
                <Item Key="client_id">Mangal</Item>
                <Item Key="ValidTokenIssuerPrefixes">https://login.microsoftonline.com</Item>
            </Metadata>
            <Cryptograph>
                <Key Id="client_secret" StorageReferenceId="key" />
            </Cryptograph>
            <IncludeTechnicalProfile ReferenceId="MIDC-Shared" />
        </TechnicalProfile>
    </TechnicalProfiles>
</ClaimsProvider>
"@

我想把这个子节点添加到这个文件中(文件名:permissions.xml

<TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"">
  <BasePolicy>
  </BasePolicy>
  <BuildingBlocks></BuildingBlocks>
  <ClaimsProviders>
    <ClaimsProvider>
    <Bomain> hey there 1</Bomain>
    <Name>Login using </Name>
    <TechnicalProfiles>
        <TechnicalProfile Id="MIDC-What">
            <DisplayName> Employee</DisplayName>
            <Description>Login with your account</Description>
            <Protocol Name="OpenIdConnect" />
            <Metadata>
                <Item Key="METADATA">https://login.microsoftonline.com</Item>
                <Item Key="client_id">Mangal</Item>
                <Item Key="ValidTokenIssuerPrefixes">https://login.microsoftonline.com</Item>
            </Metadata>
            <Cryptograph>
                <Key Id="client_secret" StorageReferenceId="key" />
            </Cryptograph>
            <IncludeTechnicalProfile ReferenceId="MIDC-Shared" />
        </TechnicalProfile>
    </TechnicalProfiles>
</ClaimsProvider>
    <ClaimsProvider>
    <Bomain> hey there 2</Bomain>
    <Name>Login using </Name>
    <TechnicalProfiles>
        <TechnicalProfile Id="MIDC-What">
            <DisplayName> Employee</DisplayName>
            <Description>Login with your account</Description>
            <Protocol Name="OpenIdConnect" />
            <Metadata>
                <Item Key="METADATA">https://login.microsoftonline.com</Item>
                <Item Key="client_id">Mangal</Item>
                <Item Key="ValidTokenIssuerPrefixes">https://login.microsoftonline.com</Item>
            </Metadata>
            <Cryptograph>
                <Key Id="client_secret" StorageReferenceId="key" />
            </Cryptograph>
            <IncludeTechnicalProfile ReferenceId="MIDC-Shared" />
        </TechnicalProfile>
    </TechnicalProfiles>
</ClaimsProvider>
    <ClaimsProvider>
    <Bomain> hey there 3</Bomain>
    <Name>Login using </Name>
    <TechnicalProfiles>
        <TechnicalProfile Id="MIDC-What">
            <DisplayName> Employee</DisplayName>
            <Description>Login with your account</Description>
            <Protocol Name="OpenIdConnect" />
            <Metadata>
                <Item Key="METADATA">https://login.microsoftonline.com</Item>
                <Item Key="client_id">Mangal</Item>
                <Item Key="ValidTokenIssuerPrefixes">https://login.microsoftonline.com</Item>
            </Metadata>
            <Cryptograph>
                <Key Id="client_secret" StorageReferenceId="key" />
            </Cryptograph>
            <IncludeTechnicalProfile ReferenceId="MIDC-Shared" />
        </TechnicalProfile>
    </TechnicalProfiles>
</ClaimsProvider>
  </ClaimsProviders>
</TrustFrameworkPolicy>

我现在正在这样做:

  1. 在变量中保存文档
$doc = [xml](Get-Content permissions.xml)
  1. 将子变量附加到 $doc xml
[Void]$doc.TrustFrameworkPolicy.ClaimsProviders.AppendChild($childxml)

我收到以下错误:

使用“1”参数调用“AppendChild”的异常:“指定的节点不能作为该节点的有效子节点插入,因为指定的节点类型错误。”

【问题讨论】:

  • 为什么说powershell 5是你正在使用的版本……然后为v2、v3和v4添加标签?
  • @Lee_Dailey 抱歉改了。
  • 谢谢!我很困惑...... [咧嘴笑]

标签: xml powershell azure-devops azure-powershell powershell-5.0


【解决方案1】:

您不能从不同的 XML 文档附加 XmlNode。相反,您必须从要附加到的文档中创建一个节点:

$childxml = $doc.CreateDocumentFragment()
$childxml.InnerXml = @'
<ClaimsProvider>
    <Bomain> hey there</Bomain>
    <Name>Login using </Name>
    <TechnicalProfiles>
        <TechnicalProfile Id="MIDC-What">
            <DisplayName> Employee</DisplayName>
            <Description>Login with your account</Description>
            <Protocol Name="OpenIdConnect" />
            <Metadata>
                <Item Key="METADATA">https://login.microsoftonline.com</Item>
                <Item Key="client_id">Mangal</Item>
                <Item Key="ValidTokenIssuerPrefixes">https://login.microsoftonline.com</Item>
            </Metadata>
            <Cryptograph>
                <Key Id="client_secret" StorageReferenceId="key" />
            </Cryptograph>
            <IncludeTechnicalProfile ReferenceId="MIDC-Shared" />
        </TechnicalProfile>
    </TechnicalProfiles>
</ClaimsProvider>
'@

[Void]$doc.TrustFrameworkPolicy.ClaimsProviders.AppendChild($childxml)

另请参阅:Append XML string block to existing XmlDocument

【讨论】:

  • 非常感谢@zett42 它就像一个魅力!
  • 一件事,我总是在输出中得到子元素 as&lt;ClaimsProvider xmlns=""&gt; ... &lt;/ClaimsProvider&gt;。我希望它是普通的香草&lt;ClaimsProvider&gt;....&lt;/ClaimsProvider&gt;。对此有什么建议吗?感谢您如此迅速的回复:-)
  • 知道了。添加了这个 $doc.OuterXml.Replace(" xmlns=""", "") 处理这些愚蠢的视觉代码添加
  • @AshwinAgarkhed 奇怪,它没有为我插入 xmlns=""。当目标文档包含xmlns= 属性时,可能会发生这种情况。在这种情况下,修复它的干净方法是:targetElement.AppendChild( $childxml, "InsertNameSpaceUriOfDocument")。该函数将检测到您正在插入与目标元素具有相同命名空间的内容并省略xmlns= 属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多