相关疑难解决方法(0)

删除appendChild添加的不需要的(空)xmlns属性

我有这个代码:

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

  $project = $root.Project;

  $beforeBuild = $root.CreateElement("Target", "");
  $beforeBuild.SetAttribute("name", "BeforeBuild");
  $beforeBuild.RemoveAttribute("xmlns");
  $project.AppendChild($beforeBuild);

  $root.Save($projectFile);
}
Run Code Online (Sandbox Code Playgroud)

应该<Target name="BeforeBuild" />为XML文档添加一个新内容.

但它也添加了一个xmlns=""我不想要的空属性.(它实际上是Visual Studio,它不喜欢这个属性!)

<Target name="BeforeBuild" xmlns="" />
Run Code Online (Sandbox Code Playgroud)

我已经尝试过这段代码:

$beforeBuild.RemoveAttribute("xmlns");
$project.AppendChild($beforeBuild);
$beforeBuild.RemoveAttribute("xmlns");
Run Code Online (Sandbox Code Playgroud)

xml powershell dom appendchild

18
推荐指数
3
解决办法
2万
查看次数

标签 统计

appendchild ×1

dom ×1

powershell ×1

xml ×1