【问题标题】:How to parse a VS2013 csproj XML file with Powershell to modify the PreBuildEvent如何使用 Powershell 解析 VS2013 csproj XML 文件以修改 PreBuildEvent
【发布时间】:2015-11-23 04:10:26
【问题描述】:

我一直在阅读 Xpath 查询(我认为是 dotnet 中使用的版本 2,但我仍然无法让我的脚本能够设置 xml 区域的内容。我该如何在 powershell 中做到这一点? 谢谢!

函数获取 XmlCsproj { $toolsPath = "C:\" [xml] $axml= 获取内容-路径“$toolsPath\csproj03.csproj” # 这会输出正确的字符串; “某物”,但不能将其设置为等于某物 $axml.Project.PropertyGroup.PreBuildEvent # 零来自 ; $axml.SelectSingleNode("/Project/PropertyGroup/PreBuildEvent") }

【问题讨论】:

    标签: xml powershell xpath


    【解决方案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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-02
      • 2020-12-30
      • 2014-06-26
      • 2013-08-31
      • 2020-10-22
      • 2020-12-20
      • 1970-01-01
      相关资源
      最近更新 更多