关于this previous question我正在尝试创建一个批处理文件,该文件必须删除并添加对XML * .csproj文件的引用。我查看过this,this,this和this之前的问题,但作为一个powershell n00b无法让它工作(到目前为止)。
任何人都可以帮我解决以下问题吗?我想删除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>
基本上我想:
作为一个非常简单的示例,我尝试了以下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)
我做错了什么?欢迎提出任何意见/建议:)
答案 0 :(得分:25)
我认为问题是你的XML文件有一个默认的命名空间xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
。这会导致XPath出现问题。所以XPath //ProjectReference
将返回0个节点。有两种方法可以解决这个问题:
以下是如何使用命名空间管理器:
$nsmgr = New-Object System.Xml.XmlNamespaceManager -ArgumentList $proj.NameTable
$nsmgr.AddNamespace('a','http://schemas.microsoft.com/developer/msbuild/2003')
$nodes = $proj.SelectNodes('//a:ProjectReference', $nsmgr)
或者:
Select-Xml '//a:ProjectReference' -Namespace $nsmgr
以下是如何使用名称空间无关的XPath:
$nodes = $proj.SelectNodes('//*[local-name()="ProjectReference"]')
或者:
$nodes = Select-Xml '//*[local-name()="ProjectReference"]'
第二种方法可能很危险,因为如果有多个命名空间,它可能会选择错误的节点,但不是你的情况。
答案 1 :(得分:22)
为了后人的缘故,我将提供完整的powershell脚本来添加和删除对csproj文件的引用。 如果你找到这个有用的,请立即投票给Andy Arismedi,因为他帮助我找到它。当你在它的时候,随意给我+1; - )
<强> AddReference.ps1 强>
# Calling convension:
# AddReference.PS1 "Mycsproj.csproj",
# "MyNewDllToReference.dll",
# "MyNewDllToReference"
param([String]$path, [String]$dllRef, [String]$refName)
$proj = [xml](Get-Content $path)
[System.Console]::WriteLine("")
[System.Console]::WriteLine("AddReference {0} on {1}", $refName, $path)
# Create the following hierarchy
# <Reference Include='{0}'>
# <HintPath>{1}</HintPath>
# </Reference>
# where (0) is $refName and {1} is $dllRef
$xmlns = "http://schemas.microsoft.com/developer/msbuild/2003"
$itemGroup = $proj.CreateElement("ItemGroup", $xmlns);
$proj.Project.AppendChild($itemGroup);
$referenceNode = $proj.CreateElement("Reference", $xmlns);
$referenceNode.SetAttribute("Include", $refName);
$itemGroup.AppendChild($referenceNode)
$hintPath = $proj.CreateElement("HintPath", $xmlns);
$hintPath.InnerXml = $dllRef
$referenceNode.AppendChild($hintPath)
$proj.Save($path)
<强> RemoveReference.ps1 强>
# Calling Convention
# RemoveReference.ps1 "MyCsProj.csproj"
# "..\SomeDirectory\SomeProjectReferenceToRemove.dll"
param($path, $Reference)
$XPath = [string]::Format("//a:ProjectReference[@Include='{0}']", $Reference)
[System.Console]::WriteLine("");
[System.Console]::WriteLine("XPATH IS {0}", $XPath)
[System.Console]::WriteLine("");
$proj = [xml](Get-Content $path)
[System.Console]::WriteLine("Loaded project {0} into {1}", $path, $proj)
[System.Xml.XmlNamespaceManager] $nsmgr = $proj.NameTable
$nsmgr.AddNamespace('a','http://schemas.microsoft.com/developer/msbuild/2003')
$node = $proj.SelectSingleNode($XPath, $nsmgr)
if (!$node)
{
[System.Console]::WriteLine("");
[System.Console]::WriteLine("Cannot find node with XPath {0}", $XPath)
[System.Console]::WriteLine("");
exit
}
[System.Console]::WriteLine("Removing node {0}", $node)
$node.ParentNode.RemoveChild($node);
$proj.Save($path)