gpt4 book ai didi

xml - 使用 PowerShell 将 system.xml.xml 元素转换为 system.xml.xml 文档

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:28 28 4
gpt4 key购买 nike

我正在编写一个我了解有限的历史脚本。

对象 A 的类型为 system.xml.xmlelement,我需要将其转换为类型 system.xml.xmldocument 以与对象 B 进行比较(类型 system.xml.xmldocument).

脚本当前尝试进行直接转换,抛出:

Cannot convert value System.Xml.XmlElement to type System.Xml.XmlDocument. Error: "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type."

我想我需要创建一个新的 system.xml.xmldocument 对象并将节点从对象 A 导入到新对象中,然后对新对象与对象 B 进行比较。我我正在努力使用正确的语法,而且我不确定这是正确的方法。

如有任何指导或帮助,我们将不胜感激。

对象 A (xmlElement) 如下所示:

<Resource xmlns="http://schemas.microsoft.com/windowsazure">
<ResourceProviderNamespace>cacheservice</ResourceProviderNamespace>
<Type>Caching</Type>
<Name>xxx</Name>
<SchemaVersion>1.0</SchemaVersion>
<ETag>xxx</ETag>
<State>Started</State>
<SubState>Active</SubState>
<UsageMeters />
<IntrinsicSettings>
<CacheServiceInput xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SkuType>Basic</SkuType>
<Location>North Europe</Location>
<SkuCount>1</SkuCount>
<ServiceVersion>1.3.0</ServiceVersion>
<ObjectSizeInBytes>1024</ObjectSizeInBytes>
<NamedCaches>
<NamedCache>
<CacheName>default</CacheName><NotificationsEnabled>false</NotificationsEnabled>
<HighAvailabilityEnabled>false</HighAvailabilityEnabled>
<EvictionPolicy>LeastRecentlyUsed</EvictionPolicy>
<ExpirationSettings>
<TimeToLiveInMinutes>10</TimeToLiveInMinutes>
<Type>Absolute</Type>
</ExpirationSettings>
</NamedCache>
</NamedCaches>
</CacheServiceInput>
</IntrinsicSettings>
<OutputItems>
<OutputItem>
<Key>CreationDate</Key>
<Value>9/30/2014 9:46:42 AM +00:00</Value>
</OutputItem>
</OutputItems>
<OperationStatus>
<Type>Create</Type>
<Result>Succeeded</Result>
</OperationStatus>
<Label />
</Resource>

对象 B (xmldocument) 如下所示:

<Resource>
<IntrinsicSettings>
<CacheServiceInput xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SkuType>Basic</SkuType>
<Location>North Europe</Location>
<SkuCount>1</SkuCount>
<ServiceVersion>1.3.0</ServiceVersion>
<ObjectSizeInBytes>134217728</ObjectSizeInBytes>
<NamedCaches>
<NamedCache>
<CacheName>default</CacheName>
<NotificationsEnabled>True</NotificationsEnabled>
<HighAvailabilityEnabled>True</HighAvailabilityEnabled>
<EvictionPolicy>True</EvictionPolicy><ExpirationSettings>
<TimeToLiveInMinutes>10</TimeToLiveInMinutes>
<Type>Absolute</Type>
</ExpirationSettings>
</NamedCache>
</NamedCaches>
</CacheServiceInput>
</IntrinsicSettings>
</Resource>

最佳答案

我知道这是一个旧问题,但由于没有人回答,所以在遇到类似问题后,我想我会分享这个。基本上,您不能将 XmlElement 隐式转换为 XmlDocument,但可以包装它。以下语法简单地做到了这一点:

给定以下虚拟 xml

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://dummy">
<CommonRoles>
<Role>Role1</Role>
<Role>Role2</Role>
<Role>Role3</Role>
</CommonRoles>
<SomethingElse>
</SomethingElse>
</configuration>

我们可以获得一个子集并将其转换为如下文档:

$value = [xml](Get-Content(Join-Path $filePath $roles[0]))
$commonRoles = $value.configuration.CommonRoles
$xml = New-Object -TypeName xml
$xml.AppendChild($xml.ImportNode($commonRoles, $true)) | Out-Null

在这种情况下,我们从文件中读取 xml 源,然后选择一个嵌套元素 (CommonRoles),它成为我们的 XmlElement 对象。后续行将创建一个新的 xml 对象,并将 XmlElement 附加到该对象。我们需要使用 ImportNode 方法,因为 xml 当前属于另一个文档,因此您需要允许它成为新文档的一部分。

Out-Null 的管道防止对 AppendChild 的调用成为函数输出的一部分。

关于xml - 使用 PowerShell 将 system.xml.xml 元素转换为 system.xml.xml 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138664/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com