powershell-将webrequest结果转换为XML时出错

powershell-将webrequest结果转换为XML时出错,xml,powershell,unicode,byte-order-mark,Xml,Powershell,Unicode,Byte Order Mark,我想处理XML文件,如下所示: <?xml version="1.0" encoding="utf-8"?> <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/

我想处理XML文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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">
  <ns:Owner>
    <ns:FSRAR_ID>3463047</ns:FSRAR_ID>
  </ns:Owner>
  <ns:Document>
    <ns:Ticket>
      <tc:TicketDate>2018-02-08T17:29:09.3041792</tc:TicketDate>
      <tc:Identity />
      <tc:DocId>2E47720A-58BC-4AF0-9159-5AD6A40C9792</tc:DocId>
      <tc:TransportId>38d9de5a-dd41-44ca-9ef3-0e404fef4bce</tc:TransportId>
      <tc:RegID>A13322D0-CB7F-455B-A7C6-C0CDC2AA4471</tc:RegID>
      <tc:DocHash />
      <tc:DocType>AsiiuTimesign</tc:DocType>
      <tc:Result>
        <tc:Conclusion>Accepted</tc:Conclusion>
        <tc:ConclusionDate>2018-02-08T17:29:09.3041792</tc:ConclusionDate>
        <tc:Comments>Документ успешно принят системой на обработку.</tc:Comments>
      </tc:Result>
    </ns:Ticket>
  </ns:Document>
</ns:Documents>
我得到的是一个错误:

Cannot convert value "<xml body goes here>" 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
无法将值“”转换为类型“System.Xml.XmlDocument”。错误:“指定的节点无法作为此节点的有效子节点插入,因为
指定的节点类型错误。“
第5行字符:1
+$webpage=([xml]((调用WebRequest-TimeoutSec 60$url.Content))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:InvalidArgument:(:)[],运行时异常
+FullyQualifiedErrorId:InvalidCastToXmlDocument
有人能建议我为什么不能将Invoke WebRequest的结果转换为XML类型,以及如何处理它吗

编辑

好的,我试着将请求内容保存到文件并在HEX编辑器中查看它,现在我看到该文件以
ff-fe-ff-fe
开头。我还对另一个URL执行了另一个请求,结果的内容是将ok转换为XML,结果它在开始时只有
ff fe
。我首先从文件中删除了
ff fe
,在脚本中读取它,并成功地将其转换为XML


现在我看到响应的内容本身就有BOM,但当我运行($response.content)时,我得到一个包含两个BOM的字符串。为什么以及如何用一个BOM表获得正确的字符串?

如果您删除了
[xml]
…似乎响应不是有效的xml。@Hackerman,为什么无效?您能从您的请求中删除
[xml]
,并将响应内容添加到您的问题中吗?@ViktorOzerov是否有机会发布它?只是为了澄清这不是无效的…@Clijsters,添加了实际的XML
Cannot convert value "<xml body goes here>" 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