使用PowerShell将system.xml.xmlelement转换为system.xml.xmldocument

时间:2014-10-01 09:54:43

标签: xml powershell

我正在制作一部历史剧本,我对此知之甚少。

对象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>

1 个答案:

答案 0 :(得分:0)

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

相关问题