使用 PowerShell 将 system.xml.xmlelement 转换为 system.xml.xmldocument [英] Converting system.xml.xmlelement to system.xml.xmldocument with PowerShell

查看:72
本文介绍了使用 PowerShell 将 system.xml.xmlelement 转换为 system.xml.xmldocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个我所知有限的历史脚本.

I'm working on an historical script which I have limited knowledge of.

对象 A 的类型为 system.xml.xmlelement,我需要将其转换为类型 system.xml.xmldocument 以与对象 B(类型 <代码>system.xml.xmldocument).

Object A is of type system.xml.xmlelement and I need to convert this to type system.xml.xmldocument to do a comparison with Object B (type system.xml.xmldocument).

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

The script currently tries to do a direct conversion which throws with:

无法将值 System.Xml.XmlElement 转换为类型 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 进行比较.我'我正在努力使用正确的语法,而且我不确定这是正确的方法.

I think I need to create a new system.xml.xmldocument object and import the node from object A into the new object and then do the comparison on the new object with object B. I'm struggling with the correct syntax, plus I'm not sure this is the correct approach.

任何指导或帮助将不胜感激.

Any guidance or help would be appreciated.

对象 A (xmlElement) 如下所示:

Object A (xmlElement) looks like this:

<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) 看起来像这样:

Object B (xmldocument) looks like this:

<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,但您可以包装它.下面的语法很简单:

I know this is an old one, but as no one has answered, I thought I would share this, after encountering a similar problem. Basically, you cannot implicitly convert the XmlElement to an XmlDocument, but you can wrap it. The following syntax does this simply:

给定以下虚拟 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>

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

We could get a subset and convert it to a document as follows:

$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 当前属于另一个文档,因此您需要允许它成为新文档的一部分.

In this case, we are reading the xml source from file, then selecting a nested element (CommonRoles) which becomes our XmlElement object. The subsequent lines will create a new xml object, and append the XmlElement to that object. We need to use the ImportNode method as the xml currently belongs to another document, so you need to allow it to become part of the new document.

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

The pipe to Out-Null prevents the call to AppendChild becoming part of the function output.

这篇关于使用 PowerShell 将 system.xml.xmlelement 转换为 system.xml.xmldocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆