PowerShellを使用してSharePointドキュメントライブラリからxmlファイルを操作しようとしています。残念ながら、それを開いてxmlにキャストすることはできません。:(ドキュメントライブラリとローカルのハードドライブに同じファイルがあります。ハードドライブから開くと、すべて問題ありません。Sharepointドキュメントライブラリから開くと、先頭に余分な文字があるため、xmlへのキャストが失敗します。 file。$splistitem.File.OpenBinary()とget-content C:.... \ file.xmlの結果をバイナリで比較しました。問題は、バイトから文字列を取得することです。利用可能なすべてを試しました。エンコーディングが機能しませんでした。私のコードは次のとおりです。
# Add SharePoint snapin if needed
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.Powershell
}
...
$splistitem = ...
...
$encode = New-Object System.Text.UTF8Encoding
[xml]$xmlFromSharepoint = $encode.GetString($splistitem.File.OpenBinary()) #throws exception
[xml]$xmlFromFile = get-content C:\....\file.xml #works fine
$web.Dispose()
Write-Host "Press any key to close ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
スローされる例外は次のメッセージです。
Cannot convert value "?<?xml version="1.0" encoding="utf-8"?>
..." to type "System.Xml.XmlDocument". Error: "Data at the
root level is invalid. Line 1, position 1."
At C:\aaa.ps1:14 char:24
+ [xml]$xmlFromSharepoint <<<< = $encode.GetString($splistitem.File.OpenBinary
())
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMet
adataException
+ FullyQualifiedErrorId : RuntimeException