1

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

}
AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48

1 Answers1

2

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?

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137