删除由appendChild添加的不需要的(空)xmlns属性 [英] Remove unwanted (empty) xmlns attribute added by appendChild

查看:189
本文介绍了删除由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);
}

应该添加一个新的< Target name =BeforeBuild/> 到XML文档。

It should add a new <Target name="BeforeBuild" /> to the XML document.

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

But it also adds an empty xmlns="" attribute which I don't want. (It's actually Visual Studio which doesn't like this attribute!)

<Target name="BeforeBuild" xmlns="" />

我已经尝试过这个代码:

I've already tried this code:

$beforeBuild.RemoveAttribute("xmlns");
$project.AppendChild($beforeBuild);
$beforeBuild.RemoveAttribute("xmlns");


推荐答案

检查这些可能的解决方案:

Check these for possible solutions:

Powershell和csproj

Xml命名空间和C#csproj

这是第二个针对OP的解决方案的解决方法:

Here is a workaround from the second solution that worked for OP:

$content = [xml] $content.OuterXml.Replace(" xmlns=`"`"", "")
$content.Save($_.FullName);

这篇关于删除由appendChild添加的不需要的(空)xmlns属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆