如何使用Powershell解析VS2013 csproj XML文件以修改PreBuildEvent

时间:2015-11-23 04:10:27

标签: xml powershell xpath

我一直在阅读Xpath查询(我认为在dotnet中使用的版本2,我仍然无法让我的脚本能够设置xml区域的内容。我怎样才能在PowerShell中执行此操作? 谢谢!

function Get-XmlCsproj {
$toolsPath = "C:\"
[xml] $axml= Get-Content -Path "$toolsPath\csproj03.csproj"

# this outputs the right string ; "SOMETHING", but can't set it equal to a thing 
$axml.Project.PropertyGroup.PreBuildEvent 

# nil from ; 
$axml.SelectSingleNode( "/Project/PropertyGroup/PreBuildEvent")

}

1 个答案:

答案 0 :(得分:1)

VS csproj文件中包含默认命名空间。因此除了明确指定的所有元素都在该命名空间中。您可以尝试这种方式使用XPath查询命名空间中的元素:

.....
# nil from ; 
$ns = new-object Xml.XmlNamespaceManager $xml.NameTable
$ns.AddNamespace("d", "http://schemas.microsoft.com/developer/msbuild/2003")
$axml.SelectSingleNode( "/d:Project/d:PropertyGroup/d:PreBuildEvent")

相关:What is the syntax for accessing child nodes using System.Xml.XmlDocument.SelectNodes with a namespace?

相关问题