4

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

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:

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."

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.

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>

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>
0

1 Answer 1

3

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:

Given the following dummy 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

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.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.