En relación con this previous question Estoy tratando de crear un archivo por lotes que como parte debe eliminar y agregar una referencia a un archivo XML * .csproj. He mirado this, this, this y this pregunta previa pero como un powershell n00b soy incapaz de hacerlo funcionar (hasta ahora).¿Cómo uso Powershell para agregar/eliminar referencias a un csproj?
¿Alguien me puede ayudar con lo siguiente? Quiero eliminar dos referencias específicas en un archivo VS2010 csproj (XML) y agregar una nueva referencia.
me abrió la csproj y la referencia se puede encontrar en la siguiente ubicación
<?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>
Básicamente quiero:
- eliminar estas dos referencias
- insertar una nueva referencia a un pre -Compiled DLL especificado por la ruta relativa
- O Agregue una ubicación de referencia de conjunto al proyecto especificado por la ruta relativa
Como un ejemplo muy simple, he intentado el siguiente script de PowerShell para eliminar todos los nodos ProjectReference. Paso el camino a csproj como argumento. Me aparece el error Cannot validate the argument 'XML'. The Argument is null or empty
. Puedo confirmar que es cargando el csproj y guardándolo in situ sin modificar para que la ruta sea correcta.
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)
¿Qué estoy haciendo mal? Cualquier comentario/sugerencia bienvenida :)
Wow, muy ¡servicial! Déjame probar esto. Gracias –
primer intento, llamé 'proj.NameTable 'y' nsmgr.AddNamespace' y obtuve 'System.Xml.Nametable' no contiene un método llamado AddNamespace' –
duh' [System.Xml.XmlNamespaceManager] $ nsmgr = $ proj.NameTable' funciona. Ok trabajando a través de esto para eliminar los nodos. Parece estar seleccionando nodos sin error –