Remove unwanted (empty) xmlns attribute added by appendChild

后端 未结 6 1631
失恋的感觉
失恋的感觉 2020-12-18 18:32

I have this code:

function setupProject($projectFile) {
  [xml]$root = Get-Content $projectFile;

  $project = $root.Project;

  $beforeBuild = $root.CreateE         


        
6条回答
  •  -上瘾入骨i
    2020-12-18 19:10

    As answered by Michael Kay, the best way to remove this unwanted namespace is creating the new child element in the same namespace as its parent:

    function setupProject($projectFile) {
      [xml]$root = Get-Content $projectFile;
    
      $project = $root.Project;
    
      # UPDATE THIS LINE $beforeBuild = $root.CreateElement("Target", "");
      $beforeBuild = $root.CreateElement("Target", $project.NamespaceURI);
      $beforeBuild.SetAttribute("name", "BeforeBuild");
      $beforeBuild.RemoveAttribute("xmlns");
      $project.AppendChild($beforeBuild);
    
      $root.Save($projectFile);
    }
    

提交回复
热议问题