2015-11-23 57 views
1

我一直在阅读Xpath查询(我认为在dotnet中使用的版本2,并且仍然无法让我的脚本能够设置一个XML的区域。我怎样才能做到这一点在PowerShell中? 谢谢!如何使用Powershell解析VS2013 csproj XML文件以修改PreBuildEvent

 
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

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?

+0

我会去试试,ty! – AnneTheAgile