關於this previous question我想創建一個批處理文件,作爲部分必須刪除並添加一個對XML * .csproj文件的引用。我已經看過this,this,this和this上一個問題,但作爲PowerShell的n00b我無法得到它的工作(到目前爲止)。如何使用Powershell添加/刪除對csproj的引用?
任何人都可以幫助我以下嗎?我想在VS2010 csproj文件(XML)中刪除兩個特定的引用並添加一個新的引用。
我打開的csproj和參考可在以下位置找到
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- ... -->
<!-- Omitted for brevity -->
<!-- ... -->
<ItemGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'">
<AvailableItemName Include="Effect" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SomeDirectory\SomeProjectFile.csproj">
<Project>{AAB784E4-F8C6-4324-ABC0-6E9E0F73E575}</Project>
<Name>SomeProject</Name>
</ProjectReference>
<ProjectReference Include="..\AnotherDirectory\AnotherProjectFile.csproj">
<Project>{B0AA6A94-6784-4221-81F0-244A68C374C0}</Project>
<Name>AnotherProject</Name>
</ProjectReference>
</ItemGroup>
<!-- ... -->
<!-- Omitted for brevity -->
<!-- ... -->
</Project>
基本上我想:
- 刪除這兩個引用
- 插入一個新的參照預 - 由相對路徑指定的編譯DLL
- 或將組件參考位置添加到由相對路徑指定的項目
作爲一個非常簡單的例子,我嘗試了下面的PowerShell腳本來刪除所有的ProjectReference節點。我將路徑傳遞給csproj作爲參數。我收到錯誤Cannot validate the argument 'XML'. The Argument is null or empty
。我可以確認它是加載csproj並將其保存在原地而未修改,因此路徑是正確的。
param($path)
$MsbNS = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}
function RemoveElement([xml]$Project, [string]$XPath, [switch]$SingleNode)
{
$xml | Select-Xml -XPath $XPath | ForEach-Object{$_.Node.ParentNode.RemoveAll()}
}
$proj = [xml](Get-Content $path)
[System.Console]::WriteLine("Loaded project {0} into {1}", $path, $proj)
RemoveElement $proj "//ProjectReference" -SingleNode
# Also tried
# RemoveElement $proj "/Project/ItemGroup/ProjectReference[@Include=`'..\SomeDirectory\SomeProjectFile.csproj`']" -SingleNode
# but complains cannot find XPath
$proj.Save($path)
我在做什麼錯?任何意見/建議歡迎:)
哇塞,太有幫助!讓我試試這個。謝謝 –
第一次嘗試,我調用了'proj.NameTable'和'nsmgr.AddNamespace',並得到'System.Xml.Nametable不包含名爲AddNamespace的方法' –
duh'[System.Xml.XmlNamespaceManager] $ nsmgr = $ proj.NameTable'的作品。確定通過這個去除節點。似乎選擇節點沒有錯誤 –