title: PHP to Powershell script
body: <p>I don't know much about PHP, but I've inherited a PHP script that I would like to convert to powershell.  I'm not sure if this is possible. But it looks like the script is getting a list of products as XML from a service.  It is then doing some cr/lf stripping, swapping a few quote characters, and then converting the xml to json.  Seems pretty straight forward.  I'm not very knowledgeable on powershell either, so I was hoping this would be a simple task for someone here.</p>
  
  <pre><code>&lt;?php
      $fileContents= file_get_contents("http://svc.mysite.com/api/GetItems");
  
      $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
  
      $fileContents = trim(str_replace('"', "'", $fileContents));
  
      $simpleXml = simplexml_load_string($fileContents);
  
      $map = json_decode(json_encode($simpleXml));
  
      print json_encode($map-&gt;item);
  ?&gt;
  </code></pre>
  
  <p>Here is a sample of the XML data</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;items xmlns="http://svc.tricorbraun.com/api/GetItems" version="1"&gt;
      &lt;item&gt;&lt;ItemID&gt;000072&lt;/ItemID&gt;&lt;ItemName&gt;Dropper Tip Closure&lt;/ItemName&gt;&lt;ItemGroup&gt;Closures&lt;/ItemGroup&gt;&lt;Shape&gt;Round&lt;/Shape&gt;&lt;Style&gt;Fitment/Plug&lt;/Style&gt;&lt;/item&gt;
      &lt;item&gt;&lt;ItemID&gt;000182&lt;/ItemID&gt;&lt;ItemName&gt;28-480, P/P Push and Turn Closure&lt;/ItemName&gt;&lt;ItemGroup&gt;Closures&lt;/ItemGroup&gt;&lt;Shape&gt;Round&lt;/Shape&gt;&lt;Style&gt;Child Resistant&lt;/Style&gt;&lt;/item&gt;
      &lt;item&gt;&lt;ItemID&gt;000187&lt;/ItemID&gt;&lt;ItemName&gt;28-480, P/P Child Resistant Closure&lt;/ItemName&gt;&lt;ItemGroup&gt;Closures&lt;/ItemGroup&gt;&lt;Shape&gt;Round&lt;/Shape&gt;&lt;Style&gt;Child Resistant&lt;/Style&gt;&lt;/item&gt;
      &lt;item&gt;&lt;ItemID&gt;000190&lt;/ItemID&gt;&lt;ItemName&gt;28mm SPEC, P/P Dispensing Closure&lt;/ItemName&gt;&lt;ItemGroup&gt;Closures&lt;/ItemGroup&gt;&lt;Shape&gt;Round&lt;/Shape&gt;&lt;Style&gt;Dispensing&lt;/Style&gt;&lt;/item&gt;
  &lt;/items&gt;
  </code></pre>
  
  <p>I came up with this, but if fails with an 'Unexpected namespace error'</p>
  
  <pre><code>    $doc = New-Object System.Xml.XmlDocument
  $memStream = new-object System.IO.MemoryStream
  $jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($memStream)
  
  $doc.Load("http://svc.mysite.com/api/GetItems")
  $doc.Save($jsonWriter)
  $bytes = $memStream.ToArray()
  
  [System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
  Write-Output $jsonWriter.ToString()
  </code></pre>