PowerShell 不允许我将 XML 节点添加到 XML 文件(使用 PowerShell 5.1.183)
PowerShell doesn't allow me to add XML node to XML file (using PowerShell 5.1.183)
从我试图将 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>
我现在正在做这个:
- 将文档保存在变量中
$doc = [xml](Get-Content permissions.xml)
- 将子变量附加到 $doc xml
[Void]$doc.TrustFrameworkPolicy.ClaimsProviders.AppendChild($childxml)
我收到以下错误:
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."
您不能附加来自不同 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
从我试图将 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>
我现在正在做这个:
- 将文档保存在变量中
$doc = [xml](Get-Content permissions.xml)
- 将子变量附加到 $doc xml
[Void]$doc.TrustFrameworkPolicy.ClaimsProviders.AppendChild($childxml)
我收到以下错误:
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."
您不能附加来自不同 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