title: powershell - error converting webrequest results into XML
body: <p>I want to process XML file something like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ns:Documents xmlns:tc="http://fsrar.ru/WEGAIS/Ticket" xmlns:oref="http://fsrar.ru/WEGAIS/ClientRef" xmlns:ns="http://fsrar.ru/WEGAIS/WB_DOC_SINGLE_01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.0"&gt;
    &lt;ns:Owner&gt;
      &lt;ns:FSRAR_ID&gt;3463047&lt;/ns:FSRAR_ID&gt;
    &lt;/ns:Owner&gt;
    &lt;ns:Document&gt;
      &lt;ns:Ticket&gt;
        &lt;tc:TicketDate&gt;2018-02-08T17:29:09.3041792&lt;/tc:TicketDate&gt;
        &lt;tc:Identity /&gt;
        &lt;tc:DocId&gt;2E47720A-58BC-4AF0-9159-5AD6A40C9792&lt;/tc:DocId&gt;
        &lt;tc:TransportId&gt;38d9de5a-dd41-44ca-9ef3-0e404fef4bce&lt;/tc:TransportId&gt;
        &lt;tc:RegID&gt;A13322D0-CB7F-455B-A7C6-C0CDC2AA4471&lt;/tc:RegID&gt;
        &lt;tc:DocHash /&gt;
        &lt;tc:DocType&gt;AsiiuTimesign&lt;/tc:DocType&gt;
        &lt;tc:Result&gt;
          &lt;tc:Conclusion&gt;Accepted&lt;/tc:Conclusion&gt;
          &lt;tc:ConclusionDate&gt;2018-02-08T17:29:09.3041792&lt;/tc:ConclusionDate&gt;
          &lt;tc:Comments&gt;Документ успешно принят системой на обработку.&lt;/tc:Comments&gt;
        &lt;/tc:Result&gt;
      &lt;/ns:Ticket&gt;
    &lt;/ns:Document&gt;
  &lt;/ns:Documents&gt;
  </code></pre>
  
  <p>Which I get as a result of Invoke-WebRequest:</p>
  
  <pre><code>$url = "http://server/queue"
  $webpage = ([xml]((Invoke-WebRequest -TimeoutSec 60 $url).Content))
  </code></pre>
  
  <p>What I get is an error:</p>
  
  <pre><code>Cannot convert value "&lt;xml body goes here&gt;" 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:5 char:1
  + $webpage = ([xml]((Invoke-WebRequest -TimeoutSec 60 $url).Content))
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvalidCastToXmlDocument
  </code></pre>
  
  <p>Can anyone suggest why can't I transform the results of Invoke-WebRequest to XML type and how to deal with it?</p>
  
  <p>EDIT</p>
  
  <p>Ok, I tried saving request contents to the file and viewing it in HEX editor, now I see that file starts with <code>ff fe ff fe</code>. I also performed another request to another URL which result's content was casting ok to XML and it turns out it had only <code>ff fe</code> at the start. I deleted first <code>ff fe</code> from the file, read it in the script and it casted to XML successfully.</p>
  
  <p>Now I see that response's content has BOM by itself but when I run ($response).Content I get a string with two BOMs. Why and how can I get correct string with just one BOM?</p>