【发布时间】:2012-05-05 17:17:38
【问题描述】:
我正在使用 powershell 2.0 来编辑很多 csproj 文件。编辑的要求之一是添加具有不同条件的新 PropertyGroup(请查看下面的示例)
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'My-New-Env|AnyCPU'">
问题是powershell为我添加的所有新PropertyGroup标签添加了空的xmlns。
例如:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'My-New-Env|AnyCPU'" xmlns="">
有什么方法可以在没有任何命名空间的情况下添加新的 xml 节点?
在添加新的 PropertyGroup 之前,我尝试使用以下代码删除命名空间属性,但它不起作用。 (这意味着该属性实际上并未被删除,并且在添加新节点后我仍然可以看到空的命名空间。)
$content = [xml](gc $_.FullName);
Write-Host "Reading "$_.FullName -foregroundcolor yellow;
$project = $content.Project;
$content.Project.RemoveAttribute("xmlns");
编辑:我正在关注这篇文章以添加新节点。
How to add new PropertyGroup to csproj from powershell
例子:
$content = [xml](gc $_.FullName);
$importNode = $content.ImportNode($configs.DocumentElement, $true)
$project = $content.Project;
$project
$project.AppendChild($importNode);
# $content.Save($_.FullName);
【问题讨论】:
-
请添加您用于添加节点的代码。
-
@Andy 谢谢。我添加了代码。
-
这是同一个问题:social.technet.microsoft.com/Forums/en-US/winserverpowershell/…(根本问题是:理解 XML 命名空间)。
标签: xml powershell