Xml 命名空间和 C# csproj

标签 xml powershell

我正在使用 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 之前,我尝试使用下面的代码删除 namespace 属性,但没有成功。 (这意味着该属性实际上并没有被删除,添加新节点后我仍然可以看到空的命名空间。)

$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);

最佳答案

查看此线程:http://bytes.com/topic/net/answers/377888-importing-nodes-without-namespace ,这似乎不容易完成,但您可以使用解决方法:

代替:

$content.Save($_.FullName);

使用:

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

关于Xml 命名空间和 C# csproj,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308598/

相关文章:

xml - 我如何使用继承(即 xs :extension) in my own xml schema?

.net - 性能:XDocument 与 XmlDocument

powershell - 如何使用 PowerShell 替换文件中的多个字符串

powershell - 在 Powershell 中对路径列表进行排序

loops - 脚本的循环部分

r - 使用 R 将 XML 转换为 JSON

AndroidManifest.xml语法检查工具?

xml - 将 XSLT 和 XML 输出到 XML 文件

powershell - 使用 powershell 在现有 CSV 中添加列 CSV

powershell - 在 CloudFormation 模板中运行 PowerShell 命令