我想用powershell來處理sharepoint文檔庫中的xml文件。不幸的是,我無法打開它並將其轉換爲xml。 :(我在文檔庫中有相同的文件,在硬盤上有本地文件當我從硬盤打開它時,一切都很好當我從Sharepoint文檔庫中打開它時,轉換爲xml失敗的原因是因爲在開頭的額外字符我比較了$ splistitem.File.OpenBinary()和get-content C:.... \ file.xml的結果,它與二進制比較是一樣的,問題是從字節中獲取字符串,我嘗試了所有可用的。編碼,但沒有奏效這裏是我的代碼:從Powershell中的Sharepoint文檔庫打開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
難道有人知道如何解決上面的代碼
在此先感謝!