2017-03-26 5 views
1

私は初心者です。このWebページでは、後になったことをするスクリプトを見つけましたが、特殊文字がある場合は*'です。私のCSVファイルのcolumn2はXMLファイルに追加されません。Powershell:foreach-objectを使ったxmlの特殊文字は一致しません。

これは、これは私のCSVファイルはcolumn1のであり、私のXMLファイルが持っているものであるスクリプト

$c = Import-Csv C:\temp\testlinks.csv 
Get-ChildItem C:\temp\output.xml | Foreach-Object { 
    # Out-String in the next line lets you work with a single string rather than an 
    # array of strings. 
    $xmldoc = (Get-Content $_.PSPath | Out-String) 

    # Loop through your CSV, updating $xmldoc with each pass 
    $c | Foreach-Object { 
    $xmldoc = $xmldoc -replace $_.column1name, ($_.column1name + " " + $_.column2name) 
    } 

    # Write the updated $xmldoc back to the original XML file's path 
    $xmldoc | Set-Content $_.PSPath 
} 

です:

<title lang="en">M*A*S*H</title> 
<title lang="en">Who&apos;s The Boss?</title> 

+1

'-replace'は正規表現をサポートしています。理論的には、文字列にはエスケープする必要がある正規表現のメタ文字があります。 'regex :: Escape()'で最初にエスケープしてください。 – Matt

+0

これは完璧に動作しました – Denz

答えて

2

-replaceパターンのための正規表現を使用していただきありがとうございます、特殊文字をエスケープする必要があります。試してください:

​​
関連する問題