In relation to this previous question I am trying to create a batch file which as part must remove and add a reference to an XML *.csproj file. I have looked at this, this, this and this previous question but as a powershell n00b am unable to get it working (so far).
Can anyone help me with the following? I want to remove two specific references in a VS2010 csproj file (XML) and add a new reference.
I opened the csproj and the reference can be found at the following location
<?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="..SomeDirectorySomeProjectFile.csproj">
<Project>{AAB784E4-F8C6-4324-ABC0-6E9E0F73E575}</Project>
<Name>SomeProject</Name>
</ProjectReference>
<ProjectReference Include="..AnotherDirectoryAnotherProjectFile.csproj">
<Project>{B0AA6A94-6784-4221-81F0-244A68C374C0}</Project>
<Name>AnotherProject</Name>
</ProjectReference>
</ItemGroup>
<!-- ... -->
<!-- Omitted for brevity -->
<!-- ... -->
</Project>
Basically I want to:
- remove these two references
- insert a new reference to a pre-compiled DLL specified by relative path
- OR Add an Assembly Reference Location to the project specified by relative path
As a very simple example I have tried the following powershell script to delete all the ProjectReference nodes. I pass the path to csproj as argument. I get the error Cannot validate the argument 'XML'. The Argument is null or empty
. I can confirm it is loading the csproj and saving it in-place unmodified so the path is correct.
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=`'..SomeDirectorySomeProjectFile.csproj`']" -SingleNode
# but complains cannot find XPath
$proj.Save($path)
What am I doing wrong? Any comments/suggestions welcome :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…