1

I have been stuck on this from a week where I am trying to append a XML node to a parent XML element. the child node and the parent node look like this :

[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>
"@

And I want to add this child node to this file (filename: 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>

I am doing this right now:

  1. Saving doc in a variable
$doc = [xml](Get-Content permissions.xml)
  1. Appending the child variable to $doc xml
[Void]$doc.TrustFrameworkPolicy.ClaimsProviders.AppendChild($childxml)

I am getting the following error:

Exception calling "AppendChild" with "1" argument(s): "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type."

3
  • why do you say powershell 5 is the version you are using ... and then add tags for v2, v3, and v4? Commented Jan 18, 2021 at 5:30
  • @Lee_Dailey sorry changed it. Commented Jan 18, 2021 at 5:33
  • thank you! i was quite confused ... [grin] Commented Jan 18, 2021 at 11:07

1 Answer 1

4

You can't append an XmlNode from a different XML document. Instead you have to create a node from the document to be appended to:

$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)

See also: Append XML string block to existing XmlDocument

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot @zett42 It works like a charm!
One thing, I always get the child element as<ClaimsProvider xmlns=""> ... </ClaimsProvider> in the output. I want it to be plain vanilla <ClaimsProvider>....</ClaimsProvider> . Any suggessions for that? Thanks for such swift replies :-)
got it. added this $doc.OuterXml.Replace(" xmlns=""", "") handels such stupid additions by visual code
@AshwinAgarkhed Strange, it didn't insert xmlns="" for me. It can happen when the target document contains a xmlns= attribute. In this case the clean way to fix it would be: targetElement.AppendChild( $childxml, "InsertNameSpaceUriOfDocument"). The function would detect that you are inserting something with same namespace as target element and omit the xmlns= attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.