简体   繁体   中英

How to parse a VS2013 csproj XML file with Powershell to modify the PreBuildEvent

I have been reading up on the Xpath queries (version 2 I think_ used in dotnet, and I still cannot get my script to be able to set the contents of an xml region. How can I do that in powershell? thank you!

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")

}

VS csproj file has default namespace in it. So all elements, except explicitly specified otherwise, are in that namespace. You can try this way to query element in namespace using 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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM