I'm executing an HTTP Get request (via Invoke-WebRequest
) against a website, that's in turn, is returning the following XML structure:
<?xml version="1.0" encoding="utf-8"?>
<ROOT xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<VENDOR>
<SUBJECT>Check Oil</SUBJECT>
<VENDORID>NA</VENDORID>
<VANTIVE>2</VANTIVE>
<MODEL>I</MODEL>
<DESCRIPTION>Success</DESCRIPTION>
</VENDOR>
<VENDOR>
<SUBJECT>Check Lights</SUBJECT>
<SIGN>N</SIGN>
<VENDORID>NA</VENDORID>
<VANTIVE>2</VANTIVE>
<MODEL>I</MODEL>
<DESCRIPTION>Success</DESCRIPTION>
</VENDOR>
<VENDOR>
<SUBJECT>Check Engine</SUBJECT>
<SIGN>N</SIGN>
<VENDORID>NA</VENDORID>
<VANTIVE>2</VANTIVE>
<MODEL>I</MODEL>
<DESCRIPTION>Success</DESCRIPTION>
</VENDOR>
</ROOT>
When trying to parse the XML data, using the ConvertTo-Xml
cmdlet:
$result = Invoke-WebRequest -Uri 'https://mycars.com/list/cars' -UseBasicParsing -Method Get
$resultinxml = $result.Content | ConvertTo-Xml
$resultinxml.Objects.Object.'#text'
Everything is working properly.
But when I'm trying to use the [xml] accelerator type instead:
$result = Invoke-WebRequest -Uri 'https://mycars.com/list/cars' -UseBasicParsing -Method Get
[xml] $resultinxml = $result.Content
The following error is occurred:
Cannot convert value "<?xml version="1.0" encoding="utf-8"?>
<ROOT xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<VENDOR>
<SUBJECT>Check Oil</SUBJECT>
<VENDORID>NA</VENDORID>
<VANTIVE>2</VANTIVE>
<MODEL>I</MODEL>
<DESCRIPTION>Success</DESCRIPTION>
</VENDOR>
<VENDOR>
<SUBJECT>Check Lights</SUBJECT>
<SIGN>N</SIGN>
<VENDORID>NA</VENDORID>
<VANTIVE>2</VANTIVE>
<MODEL>I</MODEL>
<DESCRIPTION>Success</DESCRIPTION>
</VENDOR>
<VENDOR>
<SUBJECT>Check Engine</SUBJECT>
<SIGN>N</SIGN>
<VENDORID>NA</VENDORID>
<VANTIVE>2</VANTIVE>
<MODEL>I</MODEL>
<DESCRIPTION>Success</DESCRIPTION>
</VENDOR>
</ROOT>
" 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."
At line:1 char:1
+ $webreqcontent = [xml] ($result.Content)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastToXmlDocument
Which is very odd, as the XML structure hasn't been changed and is XML valid.
Do accelerator types in PowerShell has some prerequisites or limitations, that could cause this issue?