2017-08-24 64 views
1

我目前使用TFS PowerTools在PowerShell中運行查詢並將結果導出爲XML文件。然後,我想將該XML文件導入Alteryx進行進一步處理。當TFS PT導出XML文件,它把下面的頭頂部:使用PowerShell刪除XML編碼信息

<?xml version="1.0" encoding="utf-8"?> 

然而Alteryx不喜歡這一點,並拒絕,除非該編碼信息被拿掉閱讀文檔。有沒有一種方法可以使用PowerShell刪除編碼信息,因此它只顯示:

謝謝您的幫助。

回答

0

是,您可以:

$doc = New-Object xml 
$doc.Load((Resolve-Path ".\path\to\doc.xml").ProviderPath) 
if($doc.FirstChild.NodeType -eq 'XmlDeclaration') 
{ 
    $doc.FirstChild.Encoding = $null 
} 
$doc.Save((Resolve-Path ".\path\to\doc.xml").ProviderPath) 
+0

感謝您的快速響應!我將你的代碼插入到我的腳本中並更改了文件路徑,但是當我運行腳本時出現以下錯誤: – camruny

+0

使用「1」參數調用「Load」的異常:「'。',十六進制值0x00 ,是一個無效字符。第2行,位置 1.「 – camruny

+0

在C:\ Users \ RunQuery.ps1:29 char:1 + $ doc.Load((Resolve-Path「。\ workItems.xml」)。ProviderPath) + CategoryInfo:NotSpecified:(:) [], MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException 使用「1」參數調用「保存」的異常:「無效的XML文檔。文檔沒有根元素。 在C:\ Users \ RunQuery.ps1:34 char:1 + $ doc.Save((Resolve-Path「。\ workItems.xml」)。ProviderPath) + + CategoryInfo:NotSpecified:(:) [] ,MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException – camruny

0

這將工作但XML文件將不再有效。

$xml = get-content $xmlfile | Out-String 
$xml = $xml.replace('<?xml version="1.0" encoding="utf-8"?>',"") #removes text 
$xml | out-file $xmlfile #outputs text in file 
+0

雖然這[代碼可能會回答問題](https:/ /meta.stackexchange.com/q/148272),提供了關於*爲什麼*和/或*這個代碼如何回答這個問題的額外的上下文來提高它的長期價值。 – Parfait