【问题标题】:Converting system.xml.xmlelement to system.xml.xmldocument with PowerShell使用 PowerShell 将 system.xml.xmlelement 转换为 system.xml.xmldocument
【发布时间】:2015-05-18 21:26:58
【问题描述】:

我正在编写一个我知之甚少的历史剧本。

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

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

无法将值 System.Xml.XmlElement 转换为类型 System.Xml.XmlDocument。错误:“指定的节点不能作为该节点的有效子节点插入,因为指定的节点类型错误。”

我想我需要创建一个新的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>

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    我知道这是一个旧的,但由于没有人回答,我想我会在遇到类似问题后分享这个。基本上,您不能将 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 的调用成为函数输出的一部分。

    【讨论】:

      猜你喜欢
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2021-05-20
      • 2021-09-21
      相关资源
      最近更新 更多