PowerShell 7.3 - Trouble with Parsing XML from Web Response with Encoding Issues
I'm learning this framework and I've been struggling with this for a few days now and could really use some help. I'm trying to parse an XML response from a web API using PowerShell 7.3, but I'm running into issues where the response appears to be malformed due to encoding problems. The API I'm working with returns XML data, but when I use `Invoke-RestMethod`, the XML response seems to have incorrect characters and is not parsed correctly. Hereβs the code Iβm using: ```powershell $response = Invoke-RestMethod -Uri 'https://api.example.com/getdata' -Method Get $xmlContent = [xml]$response ``` When I examine `$response`, I see that it contains some special characters, and I'm getting the following behavior when I try to access elements in the XML: ``` want to convert 'System.String' to the type 'System.Xml.XmlDocument' required by parameter 'Xml'. ``` I've also tried using `-ContentType 'application/xml'`, but it doesn't seem to help. To troubleshoot, I printed the raw response: ```powershell $response | Out-File 'response.xml' ``` Opening `response.xml` in a text editor shows that the encoding might be mismatched, and I suspect that the XML declaration is incorrect or missing. I considered using `Get-Content` with a specific encoding, like `-Encoding UTF8`, but I'm unsure how to integrate that with `Invoke-RestMethod`. Any advice on how to handle this XML properly or how to troubleshoot the encoding would be greatly appreciated! What am I doing wrong? I'm working in a Linux environment. I'd be grateful for any help.